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
madhan ravi
madhan ravi 2019 年 1 月 8 日
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?
Eldwin Cheung
Eldwin Cheung 2020 年 2 月 2 日
amazing. this guys using element wise ops. so much quicker

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by