Array doesn't get filled - for loop

hello, the arrays idx_u, idx_o, X wont get filled by the for loop, why?
idx_u = zeros(m,1);
idx_o = zeros(m,1);
X = zeros(m,1);
for r = 1:m
if nnz(gradMag(:,r)) > 0
idx_o = find(gradMag(:,r),1,'first');
idx_u = find(gradMag(:,r),1,'last');
X = r;
else
idx_o = NaN;
idx_u = NaN;
X = r;
end
end

 採用された回答

dpb
dpb 2016 年 10 月 24 日
編集済み: dpb 2016 年 10 月 24 日

0 投票

Because you didn't write the array indices inside the loop; you filled each array with a constant each pass thru the loop; the last value found will be the value remaining for all elements.
idx_u = nan(m,1); idx_o = idx_u; % fill w/ NaN initially; will overwrite finite locations
for r = 1:m
if any(gradMag(:,r))
ix=find(gradMag(:,r)); % get all locations in one call
idx_o(i)=min(ix); % I'm guessing but you can test that is as
idx_u(i)=max(ix); % fast or faster than two calls to FIND()
end
end
X=1:m; % is just the index vector; no need for inside loop

1 件のコメント

Payjay
Payjay 2016 年 10 月 25 日
of course! i got it later in the day, sorry for the obvious question! thanks, anyways!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2016 年 10 月 24 日

コメント済み:

2016 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by