MATLAB Answers

Problem with for loop

1 view (last 30 days)
YH
YH on 23 Nov 2018
Commented: YH on 26 Nov 2018
Hallo,
I have a large matrix let's say A 150 * 220000 , including columns that are set entirely to zero .
I want to find '' column wise'' the first element that is larger than 5 and the first element smaller than 200 and store them as two vectors
I creat a for loop, but it breaks when it reaches the first zero column in the matrix. so instead of idx_start 1 * 220000 , I get idx_start 1 * 15000
how can i modify my code so the loop continue over the zero columns?
and would be better if I replaced the zero columns with NaN?
I tried something with isempty but it does not work like I want.
wenn the condition is not met, it is enough to be replaced with NaN
[nx,ny] = size(A) ;
for j = 1:ny
idx_start(:,j) = find(A(:,j)> 5 ,1,'first') ;
if (isempty(A(:,j)))
continue
idx_end(:,j) = find (A(:,j) < 200 , 1, 'first');
end
end

  4 Comments

Show 1 older comment
YH
YH on 23 Nov 2018
I edited it, it was a mistake
madhan ravi
madhan ravi on 23 Nov 2018
Give a short example of your matrix and your desired output
Jan
Jan on 23 Nov 2018
isempty(A(:,j)) is always false, because it checks the number of elements, not the contents of the elements. See any().

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 23 Nov 2018
Edited: Andrei Bobrov on 23 Nov 2018
s = size(A,2);
[ii,jj] = find(cumsum(cat(3,A > 5,A < 200)) == 1);
out = accumarray([rem(jj-1,s)+1,ceil(jj/s)],ii,[s,2],[],nan);

  1 Comment

YH
YH on 26 Nov 2018
thanks! it is a great code, short, fast and works as I wanted

Sign in to comment.

More Answers (2)

Dennis
Dennis on 23 Nov 2018
The problem is that find might return an empty vector and in that case the assignment fails.
You could catch this error by checking if there are any values > 5.
if any(A(:,j)>5)
idx_start(:,j) = find(A(:,j)> 5 ,1,'first') ;
else
idx_start(:,j)=0 %or NaN or whatever you want to happen
end
I would prefer the use of only one array to store start and end values. Especially since you are always only storing 1 value you could do it like this:
if any(A(:,j)>5) && any(A(:,j)<200)
idx(1,j) = find(A(:,j)> 5 ,1,'first');
idx(2,j) = find (A(:,j) < 200 , 1, 'first');
else
idx(1,j)=0; %or NaN or whatever you want to happen
idx(2,j)=0; %or NaN or whatever you want to happen
end

  1 Comment

YH
YH on 26 Nov 2018
Thank you for your help!

Sign in to comment.


Jan
Jan on 23 Nov 2018
Edited: Jan on 23 Nov 2018
In for j=1:nx you run a loop over the rows, not columns. Do you mean ny here?
[nx, ny] = size(A);
idx_start = NaN(1, nx); % Pre-allocate!!!
idx_end = NaN(1, nx); % Pre-allocate!!!
for j = 1:ny % Or really nx?
m = find(A(:, j) > 5, 1, 'first');
if ~isempty(m)
idx_start(j) = m;
end
m = find(A(:, j) < 200, 1, 'first');
if ~isempty(m)
idx_end(j) = m;
end
end
Now all elements of the idx_... vectors are NaN, if the corresponding column of A does not contain matching elements. You can skip the search also, if the column contains zeros only:
for j = 1:ny % Or really nx?
col = A(:, j);
if any(col) % Skip if column contains zeros only
m = find(col > 5, 1, 'first');
if ~isempty(m)
idx_start(j) = m;
end
m = find(col < 200, 1, 'first');
if ~isempty(m)
idx_end(j) = m;
end
end
end

  1 Comment

YH
YH on 26 Nov 2018
thanks alot! it is really ny not nx

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!