How to create a 1x2 row vector names AB that stores A as the first entry and B as the scond entry?

조회 수: 24 (최근 30일)
A = [1; 2; 3; 4; 5]
B = [1; 2; 3; 4]
How to create a 1x2 row vector names AB that stores A as the first entry and B as the scond entry?

답변 (1개)

John D'Errico
John D'Errico 2022년 2월 20일
Since this is impossible in the most basic sense, I assume it is not homework.
Simple matrices in MATLAB cannot store a variable number of elements, or a multiple number of elements in each position.
However, it is possible. You just need to learn about the alternaitve type of arrays in MATLAB. In this case, a cell array is perfect.
A = [1; 2; 3; 4; 5];
B = [1; 2; 3; 4];
AB = {A,B}
AB = 1×2 cell array
{5×1 double} {4×1 double}
Now you can access either of those vectors simply as
AB{1}
ans = 5×1
1 2 3 4 5
AB{2}
ans = 4×1
1 2 3 4

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by