Calculation with elements in row before or after

1 vue (au cours des 30 derniers jours)
Christian S.
Christian S. le 24 Août 2020
Commenté : Bruno Luong le 24 Août 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 commentaire
Bruno Luong
Bruno Luong le 24 Août 2020
OP's answer moves here:
Thank you guys very much, that solved my problem.
You rock!

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 24 Août 2020
Modifié(e) : Bruno Luong le 24 Août 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

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by