# Don't know how to write the loop

1 view (last 30 days)
Maayan on 10 Sep 2013
Hello, I would like to address a certain column and run over the rows and check if the values in my matrix 'vector' are between some values that are is the 'tot' vector.If they are not between this values they are not copied into the 'Newvec'. The problem is that i don't know how to address all the different rows. My code works only if i have one row. Another thing importent is that a value in row number one must be between the values tot(1,1) and tot(2,1). a value in row number 3 must be between values tot(1,3) and tot (2,3). I know that my problem is only logic but i can't seem to get a hand on it. ANY help will be appreciated. Here the relevant part of my code.
for run3=1:result % result is the number of rows in the 'vector' matrix
tot(1,run3)=(ave(run3)-3*deviation(run3));
tot(2,run3)=(ave(run3)+3*deviation(run3));
end
for j=1:nFrames %nFrames is the number of columns
for index=1:result
if vector(index,j)<(tot(1,index)) && vector(index,j)>(tot(2,index)) %good
Newvec(index,j)=vector(index,j); %Newvec is the new matrix with only the values that are valid.
end
end
end
I would just like to be clearer: only if all the values in a certain column don't fulfill the condition i would like to no put them in my new matrix. Thanks!

Simon on 10 Sep 2013
Hi!
I' m a bit confused with "index" and "j" ...
Try logic operators. Your column is
col = vector(:, index)
To see if the values are in your interval:
boolvec = col < (tot(1,index)) && col > (tot(2,index));
This gives you a boolean vector. If all vector values are inside the interval, you will see that
all(boolvec)
is true.
Simon on 10 Sep 2013
If your conditions in "tot" are for rows, then you should process your matrix row-wise, like I proposed in my comment above. There is no reason to go through your columns if your condition applies to rows!

Attila on 10 Sep 2013
Edited: Attila on 10 Sep 2013
Actually, your original code works just fine. The problem is not with the loops. You should have indexed tot with 'j', not 'index'.
for run3=1:result % result is the number of rows in the 'vector' matrix
tot(1,run3)=(ave(run3)-3*deviation(run3));
tot(2,run3)=(ave(run3)+3*deviation(run3));
end
for j=1:nFrames %nFrames is the number of columns
for index=1:result
if vector(index,j)<(tot(1,j)) && vector(index,j)>(tot(2,j)) %good
Newvec(index,j)=vector(index,j); %Newvec is the new matrix with only the values that are valid.
end
end
end
##### 2 CommentsShowHide 1 older comment
Simon on 11 Sep 2013
Just to make it clearer: it doesn't matter if tot has 2 rows and a number of columns or the other way round.
If you get confused, you may transpose your "tot":
tot = tot.';
This way you have a matrix "tot" with 2 coulmuns (column 1: lower threshold, cloumn 2: upper threshold) and a number of rows equal to the number of rows in your "vector". My example above now looks like
% prepare new array as copy of input array Newvec = vector;
% run through rows for n = 1:result % this is the current row row = vector(n, :);
% this checks for the limits
boolvec = (row < (tot(n,1))) && (row > (tot(n,1)));
% boolvec has 1 (or true) where you are inside the limits and 0 (false) otherwise
% remove all values outside, set them to 0
Newvec(n, ~boolvec) = 0;
end
Please note that only "tot(1,n)" changes to "tot(n,1)" for the lower threshold (and the same for the upper threshold)