How to insert an array into a matrix?

조회 수: 1 (최근 30일)
Sedat Izcan
Sedat Izcan 2020년 12월 11일
댓글: Sedat Izcan 2020년 12월 11일
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.

답변 (1개)

KALYAN ACHARJYA
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.
:)
  댓글 수: 1
Sedat Izcan
Sedat Izcan 2020년 12월 11일
Thank you so much for your answer. :)
The real code that I use is the following:
lat = readmatrix('training_1deneme.xlsx','Range','D2347:D2404');
lon = readmatrix('training_1deneme.xlsx','Range','E2347:E2404');
mean_ini=[lat ,2 ,lon ,2]
and I need to use the matrix for every single lat and lon to multiply with 2.Therefore I need to create a code that every single matrix reads only one value from lat and lon.
eg. Assume that lat1=12,lat2=15,lon1=10,lon2=15
>>Result=
2*[12,2,10,2]
2*[15,2,15,2]
I need to obtain different matrices rather than 1 column because I will use each values in other equations as well.
Thanks again!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by