Two vectors into matrix with an opeartion?

조회 수: 1 (최근 30일)
Christian S.
Christian S. 2020년 8월 24일
편집: madhan ravi 2020년 8월 24일
Hi everybody,
I'd like to create a matrix from 2 vectors with a subtraction.
a=[0,1,2,3]
b=[6,7,8,9]
The operation is b-a, so the resulting matrix should look like this
6 5 4 3
7 6 5 4
8 7 6 5
9 8 7 6
Additional question: Is it possible to place the results underneath the first row of Vector a
0 1 2 3
6 5 4 3
7 6 5 4
8 7 6 5
9 8 7 6
Thank you for your help in advance
Christian

채택된 답변

Alan Stevens
Alan Stevens 2020년 8월 24일
Like this:
a = [0 1 2 3];
b = [6; 7; 8; 9]; % Note that b is a column vector and a is a row vector
A = repmat(a,4,1)
A =
0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3
>> m = b - A
m =
6 5 4 3
7 6 5 4
8 7 6 5
9 8 7 6
>> m = [a; m]
m =
0 1 2 3
6 5 4 3
7 6 5 4
8 7 6 5
9 8 7 6
  댓글 수: 2
madhan ravi
madhan ravi 2020년 8월 24일
using repmat() is not efficient than an implicit expansion
Christian S.
Christian S. 2020년 8월 24일
That was quick, wow! Thank you very much.
Very best
Christian

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 8월 24일
편집: madhan ravi 2020년 8월 24일
[a; b(:) - a]

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by