Getting the ratio of consecutive links in network using loop.

1 回表示 (過去 30 日間)
Niraj Bal Tamang
Niraj Bal Tamang 2022 年 6 月 12 日
編集済み: Voss 2022 年 6 月 12 日
I have two variables
A: 1 2 3 4 5 6 7 8 9
B: 2 10 5 8 12 6 14 6 2
I want to get the ratio of Bn/Bn-1 such that for A=1 is ratio is none, A=2 is B2/B1, A=3 is B3/B2 and so on.
Can anyone tell me how to do this in loop?
Thank you

採用された回答

Voss
Voss 2022 年 6 月 12 日
A = 1:9; % unused
B = [2 10 5 8 12 6 14 6 2];
B_ratio = [NaN B(2:end)./B(1:end-1)] % no loop necessary
B_ratio = 1×9
NaN 5.0000 0.5000 1.6000 1.5000 0.5000 2.3333 0.4286 0.3333
  2 件のコメント
Niraj Bal Tamang
Niraj Bal Tamang 2022 年 6 月 12 日
Thank you so much. It gave "Dimensions of arrays being concatenated are not consistent." error first. then, I added semi colon after NaN and it fixed the error.
Voss
Voss 2022 年 6 月 12 日
編集済み: Voss 2022 年 6 月 12 日
That means your B is a column vector (mine was a row vector). In any case, glad you got it working!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by