필터 지우기
필터 지우기

Problem with for loop

조회 수: 1 (최근 30일)
YH
YH 2018년 11월 23일
댓글: YH 2018년 11월 26일
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
madhan ravi
madhan ravi 2018년 11월 23일
Give a short example of your matrix and your desired output
Jan
Jan 2018년 11월 23일
isempty(A(:,j)) is always false, because it checks the number of elements, not the contents of the elements. See any().

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 11월 23일
편집: Andrei Bobrov 2018년 11월 23일
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
YH
YH 2018년 11월 26일
thanks! it is a great code, short, fast and works as I wanted

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Dennis
Dennis 2018년 11월 23일
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
YH
YH 2018년 11월 26일
Thank you for your help!

댓글을 달려면 로그인하십시오.


Jan
Jan 2018년 11월 23일
편집: Jan 2018년 11월 23일
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
YH
YH 2018년 11월 26일
thanks alot! it is really ny not nx

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by