Calculation with elements in row before or after

1 Ansicht (letzte 30 Tage)
Christian S.
Christian S. am 24 Aug. 2020
Kommentiert: Bruno Luong am 24 Aug. 2020
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 Kommentar
Bruno Luong
Bruno Luong am 24 Aug. 2020
OP's answer moves here:
Thank you guys very much, that solved my problem.
You rock!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 24 Aug. 2020
Bearbeitet: Bruno Luong am 24 Aug. 2020
>> 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

Weitere Antworten (0)

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by