[A,v] shows error: dimensions of arrays being concatenated are not consistent
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lucia
am 19 Okt. 2024
Kommentiert: Stephen23
am 19 Okt. 2024
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]
v=[1;9;9]
A=[A,v];
1 Kommentar
Stephen23
am 19 Okt. 2024
What exact size do you expect A to have? What values do you expect A to contain?
Akzeptierte Antwort
Shivam Gothi
am 19 Okt. 2024
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]
v=[1;9;9]
A=[A,v]
The above code will also give the desired output.
I hope it helps !
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!