How to vectorize this for loop with if statement

P = zeros(100,200)
%%% M is array of size 100x200
for i=1:100
for j=1:200
P(i,j) = M(i,j) - M(i,j+10);
end
end

回答(1 个)

P = zeros(100,200)
P(1,:)=10;
P(:,1)=10;
P(2:end,2:end)=M(2:end,2:end)-M(2:end,1:end-1);

类别

在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息

标签

提问:

2020-12-7

编辑:

2020-12-7

Community Treasure Hunt

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

Start Hunting!

Translated by