別の選択肢を追加するだけです:
A(cat(3, false(size(A,1),size(A,2),2), A(:,:,3)>100)) = 0;
または、次のように 3D でインデックス変数を割り当てることができます。
id(:,:,3) = A(:,:,3)>100;
A(id) = 0;
これは、はるかにクリーンな構文を持っています。
次に、いくつかの速度テストを行います。
clc, clear all
b = 250*rand(500,500, 3);
% Me 1
tic
for ii = 1:1e2
A=b;
clear id
id = cat(3, false(size(A,1),size(A,2),2), A(:,:,3)>100);
A(id) = 0;
end
toc
% Acorbe
tic
for ii = 1:1e2
A=b;
A(:,:,3) = (A(:,:,3)<100).*A(:,:,3);
end
toc
% angainor 1
tic
for ii = 1:1e2
A=b;
clear id
id = find(A(:,:,3)>100) + 2*size(A, 1)*size(A, 2);
A(id)=0;
end
toc
% Me 2
tic
for ii = 1:1e2
A=b;
clear id
id(:,:,3) = A(:,:,3)>100;
A(id) = 0;
end
toc
% angainor 2
tic
for ii = 1:1e2
A=b;
clear id
AA = reshape(A, [], 3);
AA(AA(:,3)>100,3) = 0;
A = reshape(AA, size(A,1), size(A,2), 3);
end
toc
結果:
Elapsed time is 1.612787 seconds. % me #1
Elapsed time is 1.223496 seconds. % Acorbe
Elapsed time is 1.606858 seconds. % angainor #1
Elapsed time is 1.510153 seconds. % me #2
Elapsed time is 0.964423 seconds. % angainor #2
勝者はangainorのようです:)