Calculation with elements in row before or after

1 回表示 (過去 30 日間)
Christian S.
Christian S. 2020 年 8 月 24 日
コメント済み: Bruno Luong 2020 年 8 月 24 日
Hello MATLAB Community,
I'm searching for a elegant way to calculate with matrix-elements in different rows (or columns).
For example:
a= [1 2 3 4 5; 6 7 8 9 0];
b should be a function of a row before, e.g. b=a[Row1] + [Value of Row 2].
The result would also be
b=
7 9 11 13 5
6 7 8 9 0
Or with columns e.g. b=a[Col1] + [Value of Col 2]
b=
3 5 7 9 5
13 15 17 9 0
Does anybody have a hint for me?
Very best
Christian
  1 件のコメント
Bruno Luong
Bruno Luong 2020 年 8 月 24 日
OP's answer moves here:
Thank you guys very much, that solved my problem.
You rock!

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

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 24 日
編集済み: Bruno Luong 2020 年 8 月 24 日
>> a= [1 2 3 4 5; 6 7 8 9 0];
>> a
a =
1 2 3 4 5
6 7 8 9 0
>> conv2(a,[1;1],'same')
ans =
7 9 11 13 5
6 7 8 9 0
>> conv2(a,[1,1],'same')
ans =
3 5 7 9 5
13 15 17 9 0
If you want to do other operation that @plus, use your element-wise function on
a;
arownext = [a(2:end,:); zeros(1,size(a,2))];
a + arownext % replace + with your function
or
a;
acolnext = [a(:,2:end), zeros(size(a,1),1)];
a + acolnext % replace + with your function

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by