Finding ratio between two elements of one Array

Hi there,
I have an array that looks like this:
Array =
1 2 3 4 5 6 7 8 9
I would like to find the ratio between two elements in this array. For example 1/2, 2/3, 3,4, 4,5... etc
I came up with this code
ratio = [];
for i = 1:length(Array)
ratio(i) = Array(i)/Array(i+1)
end
but the problem is that the last iteration gives me error (last index +1) :(
how can I overcome this problem. Thank you in advance

 채택된 답변

madhan ravi
madhan ravi 2019년 1월 8일

0 개 추천

for i = 1:length(Array)-1

댓글 수: 2

Ahsan
Ahsan 2019년 1월 8일
It works. Thank you
Anytime :) , see the other answer which is easier than loop.

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 8일

2 개 추천

a(1:end-1)./a(2:end) % without loop

댓글 수: 2

Katie Rae
Katie Rae 2019년 11월 15일
Can you explain what the output of this code would be?
amazing. this guys using element wise ops. so much quicker

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2019년 1월 8일

댓글:

2020년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by