How to vectorize double sum

2 次查看(过去 30 天)
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