How to vectorize double sum

2 visualizaciones (últimos 30 días)
Esegboria Osarhemen
Esegboria Osarhemen el 9 de Feb. de 2019
Comentada: Esegboria Osarhemen el 9 de Feb. de 2019
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

Respuesta aceptada

Stephen23
Stephen23 el 9 de Feb. de 2019
Editada: Stephen23 el 9 de Feb. de 2019
>> 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by