How to insert an array into a matrix?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have the following code:
lon = xlsread('datatrial.xlsx','A1:A2');
Nsteps=2
for i=1:Nsteps
P_ini=[lon, 2 ,1];
end
-Where I want to get two different matrices eg. P_ini= [A1,2,1] & [A2,2,1]
-The error I get is:Dimensions of arrays being concatenated are not consistent.
I need to do it for larger ranges therefore I would love to learn the way to do it.
Thank you.
댓글 수: 0
답변 (1개)
KALYAN ACHARJYA
2020년 12월 11일
편집: KALYAN ACHARJYA
2020년 12월 11일
In the it read the two cell elements from the excel file
lon = xlsread('datatrial.xlsx','A1:A2');
Hence the resultant "lon" will be 2x1
>> whos Ion
Name Size Bytes Class Attributes
Ion 2x1 16 double
Next within the for loop, ion horizontal concatenate with two number (scalar)
P_ini=[lon, 2 ,1];
Which is the dimention issue, as Ion is 2x1, next two numbers, how can you do that?? But yes you can do the vertical concatenate with the nnumbers, likewise
P_ini=[lon;2;1];
As a result P_ini will be 4x1 row vector.
More: In each iteration, you can save the vector result in cell array P_ini, in such case
P_ini=cell(4,1);
for
P_ini{i}=...
end
Also, you can avoid the loop here and draw two vectors directly.
:)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!