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
댓글 수: 1
Bruno Luong
2020년 8월 24일
OP's answer moves here:
Thank you guys very much, that solved my problem.
You rock!
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!