Vectorize two for loops

1 次查看(过去 30 天)
Master Blabla
Master Blabla 2020-11-19
How to vectorize this function:
for i=1:100
for j=1:200
TheOne(i,j) = i+j;
end
end

回答(2 个)

David Hill
David Hill 2020-11-19
编辑:David Hill 2020-11-19
[i,j]=meshgrid(1:100,1:200);
TheOne=reshape(sum((Png1(:,i)-Png2(:,j)).^2),100,[]);
  4 个评论
Master Blabla
Master Blabla 2020-11-19
It does not work correctly
David Hill
David Hill 2020-11-19
Made a mistake. Try this.
[i,j]=meshgrid(1:100,1:200);
TheOne=reshape(sum((Png1(:,i)-Png2(:,j)).^2),200,[])';

请先登录,再进行评论。


CHENG QIAN LAI
CHENG QIAN LAI 2020-11-24
[i,j]=ndgrid(1:100,1:200);
% 1 2 3 . . . 200
%---------------------------
% i= 1 1 1 . . . 1 | 1
% 2 2 2 . . . 2 | 2
% 3 3 3 . . . 3 | 3
% . . . | .
% . . . | .
% . . . | .
% 100 100 100 . . . 100 | 100
%---------------------------
% j= 1 2 3 . . . 200 | 1
% 1 2 3 . . . 200 | 2
% 1 2 3 . . . 200 | 3
% . . . | .
% . . . | .
% . . . | .
% 1 2 3 . . . 200 | 100
TheOne = i+j;

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by