How to insert an array into a matrix?

1 visualización (últimos 30 días)
Sedat Izcan
Sedat Izcan el 11 de Dic. de 2020
Comentada: Sedat Izcan el 11 de Dic. de 2020
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.

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 11 de Dic. de 2020
Editada: KALYAN ACHARJYA el 11 de Dic. de 2020
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 comentario
Sedat Izcan
Sedat Izcan el 11 de Dic. de 2020
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!

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by