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일

0 개 추천

X = triu(data(:)./data(:).' - 1)

댓글 수: 4

David
David 2018년 1월 18일
Hi, Thanks for this. This doesn't replicate the loop answers. Note y = x:nn.
Just reverse the slash,
X = triu(data(:).\data(:).' - 1)
David
David 2018년 1월 18일
That does it! Many thanks.
Matt J
Matt J 2018년 1월 18일
You're welcome, but please Accept-click the answer to signify that it solved your problem.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2018년 1월 18일

댓글:

2018년 1월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by