필터 지우기
필터 지우기

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by