How to impliment logical indexing faster in matlab? [closed]

Multi tool use
How to impliment logical indexing faster in matlab? [closed]
I have several matrices that I need to add to one large matrix. The large matrix (300002x50) is split up by .001 seconds and the timing for the other 49 matricies (14250x2) are roughly .02 apart, but not uniformly distributed. I have tried find
to index the entries from the smaller matrices into the larger matrix, but it was too slow. I have since tried:
find
for a = 1:length(test)
aaa = abs(AF1(:,1)-test(a,1))<10^-6;
AF1(aaa,index)=test(a,2);
end
Where test
is a 14250x2 double (time,data)
, AF1
is a 300002x50 double matrix and index
is which column in AF1
the data will be added to. It was a bit faster, but it still takes up 99.3% (29 minutes) of the time. It works how I want it, but is there any way to implement this in a faster manner?
test
(time,data)
AF1
index
AF1
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Also what is index? Is it a second loop over all columns? Or do you select multiple rows at once?
– Dennis Klopfer
Oct 14 '15 at 17:21
I have 49 data sets and they all come with their own timing (most split by .02), but some are split by .019 and .021, so its not uniform. The timings dont match up between the sets.
– mtourtelot
Oct 14 '15 at 17:22
index is which column the data will be put in. So it reads in test, which is 14250x2 (time, data), and where the time matches with AF1, it adds the data from that row in test to the 'index' column at that time
– mtourtelot
Oct 14 '15 at 17:24
1 Answer
1
Memory permitting and barring one-off case when index ~= 1
, you can use a vectorized approach with a family of useful functions that includes bsxfun
and accumarray
, well supported by find
and abs
, under the able guidance of linear indexing
-
index ~= 1
bsxfun
accumarray
find
abs
linear indexing
[R,C] = find(abs(bsxfun(@minus,AF1(:,1),test(:,1).')) < 10^-6) %//'
idx = accumarray(R,C,,@max)
AF1((index-1)*size(AF1,1) + find(idx)) = test(idx(idx~=0),2);
Memory not permitting, thou shalt commit the crime of using a loop, that would be a modified version of the existing loop, like so -
valid_mask = true(size(AF1,1),1);
for a = size(test,1):-1:1
aaa = (abs(AF1(:,1)-test(a,1))<comp_val) & valid_mask;
valid_mask(aaa) = 0;
AF1(aaa,index)=test(a,2);
end
I was not able to use bsxfun in this manner due to insufficient memory
– mtourtelot
Oct 14 '15 at 18:26
@mtourtelot Yup! I was afraid you might run into that situation :)
– Divakar
Oct 14 '15 at 18:27
@mtourtelot Check out the added codes please.
– Divakar
Oct 14 '15 at 19:05
It seemed to work, but it was quite a bit slower than the original. It was getting hung up on your third line, assigning aaa.
– mtourtelot
Oct 14 '15 at 19:33
@mtourtelot Yeah I think you are at the tripping point where any additional variable would make it slow courtesy your already existing heavy variables. So, just stick to your existing code I would say.
– Divakar
Oct 14 '15 at 20:41
How'd you end up with 50 matrices? Probably there's something to improve in that.
– Adriaan
Oct 14 '15 at 17:16