Don't know how to write the loop

1 view (last 30 days)
Maayan
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!

Accepted Answer

Simon
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.
  7 Comments
Maayan
Maayan on 10 Sep 2013
ok, i will change it. but there is still a problem with the condition:
boolvec = (col>(tot(1,n))) && (col< (tot(2,n)));
n runs through the COLUMNS of my matrix and the number of columns in tot is equal to the number of ROWS in the matrix. That's my problem.
Simon
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!

Sign in to comment.

More Answers (1)

Attila
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 Comments
Maayan
Maayan on 10 Sep 2013
It doesn't work.I wish that was my problem. tot has only 2 rows (always) and has a number of columns that is equal to the number of rows it the matrix. j runs through the columns so it will not work. It will get to a point where almost always j will be greater then the number of rows in my matrix and i will get an error.
Simon
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)

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!