Calculation with elements in row before or after

1 次查看(过去 30 天)
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

采纳的回答

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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by