How to vectorize double sum

조회 수: 3 (최근 30일)
Esegboria Osarhemen
Esegboria Osarhemen 2019년 2월 9일
댓글: Esegboria Osarhemen 2019년 2월 9일
Please how can i vectorize this double sum
N=4
x=randn(3,N);
for i=1:N-1
for j=2:N
if j>i
a=x(:,i)-x(:,j)
end
end
end

채택된 답변

Stephen23
Stephen23 2019년 2월 9일
편집: Stephen23 2019년 2월 9일
>> N = 4;
>> x = randi(9,3,N)
x =
5 4 8 9
2 9 9 8
1 1 4 1
>> F = @(c)bsxfun(@minus,x(:,c),x(:,c+1:N));
>> M = cell2mat(arrayfun(F,1:N-1,'uni',0))
M =
1 -3 -4 -4 -5 -1
-7 -7 -6 0 1 1
0 -3 0 -3 0 3

추가 답변 (0개)

카테고리

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