How do I vectorize the following code?

1 vue (au cours des 30 derniers jours)
David
David le 18 Jan 2018
Commenté : Matt J le 18 Jan 2018
Hi,
I'm having trouble vectorizing the following code. The result should be a matrix with an upper triangle.
data = 1:1:10;
nn = length(data);
for x = 1:nn
for y = x:nn
X(x,y) = (data(y) - data(x)) / data(x);
end
end
All help gratefully received!

Réponse acceptée

Matt J
Matt J le 18 Jan 2018
X = triu(data(:)./data(:).' - 1)
  4 commentaires
David
David le 18 Jan 2018
That does it! Many thanks.
Matt J
Matt J le 18 Jan 2018
You're welcome, but please Accept-click the answer to signify that it solved your problem.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical 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