[A,v] shows error: dimensions of arrays being concatenated are not consistent

조회 수: 1 (최근 30일)
Hi everyone, I'm having a little problem. I tried to create a matrix using an already existing one and a vector just by putting it next to the matrix. I expected a new matrix made by the original plus a last new line of zeros and a new column made up by the vector I added. I remember it worked in Matlab2023b, but now it shows me this error
A=[1,2; 3,4]
A = 2×2
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v=[1;9;9]
v = 3×1
1 9 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A=[A,v];
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
  댓글 수: 1
Stephen23
Stephen23 2024년 10월 19일
What exact size do you expect A to have? What values do you expect A to contain?

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

채택된 답변

Shivam Gothi
Shivam Gothi 2024년 10월 19일
Hello @Lucia,
I tried to execute the above code in MATLAB R2023b version, but it gave the same error message.
You are trying to horizontally concatenate two matrices. Therefore, the number of rows of Matrices to be concatenated should be same. Here, the number of rows of "A" matrix should be equal to the number of rows of "V" matrix.
In your case, "A" has 2 rows and "V" has 3 rows. Therefore, you cannont concatenate them. refer the below documentation to get more information about matrix concatenation:
Instead, you can do:
A=zeros(3,2); %preallocate the A matrix
A(1:2,1:2)=[1,2 ;3,4]
A = 3×2
1 2 3 4 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v=[1;9;9]
v = 3×1
1 9 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A=[A,v]
A = 3×3
1 2 1 3 4 9 0 0 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The above code will also give the desired output.
I hope it helps !

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by