Create a concatenated matrix in matlab
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I need your help. I would like to make a matrix of 15x2501 dimension. I'm using this code.
%values of Kh
Kh = [1*10.^-7 1*10.^-6.5 1*10.^-6 1*10.^-5.5 1*10.^-5 1*10.^-4.5 1*10.^-4 1*10.^-3.5 1*10.^-3 1*10.^-2.5 1*10.^-2 1*10.^-1.5 1*10.^-1 1*10.^-0.5 1*10.^0];
%
% Matrix by each value of Kh
HMC_Kh = zeros(length(Kh),2501);
for i = 1:length(Kh)
[H,ZH,h,Zh] = func_Kh(Kh(i));
HMC_Kh = [HMC_Kh; H];
end
when I run the code, matlab show me this message:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in val_Kh_feb (line 13)
HMC_Kh = [HMC_Kh; H];
The function func_Kh returns a variable H of 1x2501 dimension maximum depending of value that take Kh, or the function func_Kh can be returns a variable H of 1 x 500 dimension minimum. That is, H changes with each value that Kh takes, so the matrix can be filled with zeros after the last value of H in each row when H is 1x500 dimensional so that each row has the same dimension of 1x2501.
I hope you can help me with this error, surely it must be silly, I learning matlab.
Thanks!
1 comentario
Jan
el 12 de Feb. de 2021
Note: 1*10.^-7 is a mutliplication and an expensive power operation. 1e-7 is a cheap constant and easier to read.
Respuestas (1)
James Tursa
el 11 de Feb. de 2021
Editada: James Tursa
el 11 de Feb. de 2021
Change this
HMC_Kh = [HMC_Kh; H];
to this
HMC_Kh(i,1:numel(H)) = H;
There were two problems with your current code. The number of elements in H at each iteration is different causing the concatenation error which you already discovered. But also you were appending H to HMC_Kh at each iteration so your preallocation didn't make sense unless you really wanted the result to start with a huge block of 0's.
Ver también
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!