I want to vectorize the following (simplified) piece of code
R=[1,2,3,4];
for i = 1:3
for j = 1:4
R(i+1,j) =R(i,j) + (j);
end
end
and I'm getting stuck because the for loops are dependent on the terms of an existing matrix. Any help is gratefully appreciated. I am working in MATLAB 2016a

回答 (1 件)

Stephen23
Stephen23 2018 年 5 月 17 日
編集済み: Stephen23 2018 年 5 月 17 日

0 投票

>> R = [1,2,3,4];
>> (0:3).' * (1:numel(R)) + R
ans =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

質問済み:

2018 年 5 月 17 日

編集済み:

2018 年 5 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by