Don't know how to write the loop
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Simon
2013-9-10
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 个评论
Simon
2013-9-10
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!
更多回答(1 个)
Attila
2013-9-10
编辑:Attila
2013-9-10
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 个评论
Simon
2013-9-11
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!