Using loop to fill new matrixes

Hi,
I am making a loop in which eventually information has to be placed in two different variables. I got two matrixes, and if the content of one colomn is higher than the content of a colomn from a different matrix it should be placed into a new matrixvariable if not, in the other.
trials = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ;
15 16 17 18 19 20 21 22 23 24 25 26 27 28;
29 30 31 32 33 34 35 36 37 38 39 40 41 4 ];
saccades = [ 1 2 3 4 5 6 7 8 9 10 ;
11 12 13 14 15 16 17 18 19 20;
21 22 23 24 25 26 27 28 29 30 ];
prims = []
corrs = []
%check if value of saccades(:,2) is higer than trials(:,12), if yes, put in corrs; if no, put in prims
for k = 1:size(saccades(:,2))
if saccades(k,2) > trials(k,12)
corrs = trials(k,:);
else
prims = trials(k,:);
end
end
So, if saccades(:,2) is higer than the value of trials(:,12) I want it to be put into matrix 'corrs', if not, I want it to be in 'prims'. So far i have figured out to put in all the lines but it only saves the final line (it overwrites instead of 'saving' all lines). So I need to add one more codeline to make sure it saves all the lines.
I hope this is clear... Can anyone help me with this?
Thanks! :)

1 Comment

@Marjoleine Hakkenberg: can you please edit your question and show us the expected output for those matrices. Then we will actually have something to test our code on and check that the output is correct.

Sign in to comment.

Answers (1)

Jan
Jan on 22 May 2017
Edited: Jan on 22 May 2017
The code does exactly what is expected:
corrs = trials(k,:);
This overwrite the contents of corrs in each iteration. Better without a loop:
n = size(saccades, 1)
match = (saccades(:,2) > trials(1:n, 12))
corrs = trials(match, :);
prims = trials(~match, :);
There are more trials than saccades. The trailing trials are ignored. Is this wanted?
size(saccades(:,2)) wastes time and might be confusing: it creates the vector saccades(:,2) at first and replies its size as a vector. 1:size(saccades(:,2)) is 1:[nx1] then. The colon operator considers the first element of the vector only, which might be what you want by accident. Safer:
1:size(saccades, 1)
[EDITED] If you really have any good reasons to use a loop (although the suggested vectorized method will be much faster):
nSacc = size(saccades, 1);
prims = zeros(nSacc, size(trials, 2)); % Pre-allocate
corrs = zeros(nSacc, size(trials, 2));
iPrims = 0;
iCorrs = 0;
for k = 1:nSacc
if saccades(k,2) > trials(k,12)
iCorrs = iCorrs + 1;
corrs(iCorrs, :) = trials(k, :);
else
iPrims = iPrims + 1;
prims(iPrims, :) = trials(k,:);
end
end
corrs = corrs(1:iCorrs, :); % Crop unused memory
prims = prims(1:iPrims, :);
Note: Avoid the iterative growing, because it is extremely inefficient.

2 Comments

Hi Simon,
Thanks for your reply, however, I want to use a loop since the above is not the data the code is actually for. I have eye-tracking data which is huge and therefore i prefer using a loop. Is it then still possible?
Jan
Jan on 22 May 2017
Edited: Jan on 22 May 2017
I do not understand, why you prefer a loop here, especially if the data is "huge". If you use loops, you have to pre-allocate the output and therefore you need twice as much RAM as in the final result. This will slowdown the processing, if the array is really "huge". Nevertheless, see [EDITED] in my answer.
By the way, using emotional terms like "huge" can be confusing in the forum. Some users think that 10'000 is huge, because they cannot compute the result manually anymore, while others have a 256 GB RAM machine and process billions of iterations.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 22 May 2017

Edited:

Jan
on 22 May 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!