How do I vectorize the following code?

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

采纳的回答

Matt J
Matt J 2018-1-18
X = triu(data(:)./data(:).' - 1)
  4 个评论
Matt J
Matt J 2018-1-18
You're welcome, but please Accept-click the answer to signify that it solved your problem.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by