フィルターのクリア

vectorization of for loop

1 回表示 (過去 30 日間)
Rizwan Khan
Rizwan Khan 2022 年 7 月 20 日
コメント済み: Rizwan Khan 2022 年 7 月 23 日
function o_lmat = reassign_label(o_lmat,h,w,p,n_prev)
for a = 1:h % for loops to reassign labels
for b = 1:w
for c = 1:p
if (o_lmat(a,b,c) ~= 0)
o_lmat(a,b,c) = o_lmat(a,b,c)+n_prev;
end
end
end
end
end
I am trying to vectorize this code as:
function o_lmat = reassign_label(o_lmat,h,w,p,n_prev)
% o_lmat = zeros(h,w,p);
if (o_lmat(:,:,:) ~= 0)
o_lmat(:,:,:) = o_lmat(:,:,:)+n_prev;
end
end
It is not working correctly. Pllease find the mistake I am making.

採用された回答

Chunru
Chunru 2022 年 7 月 20 日
function o_lmat = reassign_label(o_lmat,h,w,p,n_prev)
% for a = 1:h % for loops to reassign labels
% for b = 1:w
% for c = 1:p
% if (o_lmat(a,b,c) ~= 0)
% o_lmat(a,b,c) = o_lmat(a,b,c)+n_prev;
% end
% end
% end
% end
idx = o_lmat(:) ~=0;
o_lmat(idx) = o_lmat(idx) + n_prev;
end
  1 件のコメント
Rizwan Khan
Rizwan Khan 2022 年 7 月 23 日
thanks a lot

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by