How to vectorize double sum

2 vues (au cours des 30 derniers jours)
Esegboria Osarhemen
Esegboria Osarhemen le 9 Fév 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

Réponse acceptée

Stephen23
Stephen23 le 9 Fév 2019
Modifié(e) : Stephen23 le 9 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by