Create a concatenated matrix in matlab

10 vues (au cours des 30 derniers jours)
Alvaro Sepulveda
Alvaro Sepulveda le 11 Fév 2021
Commenté : Jan le 12 Fév 2021
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 commentaire
Jan
Jan le 12 Fév 2021
Note: 1*10.^-7 is a mutliplication and an expensive power operation. 1e-7 is a cheap constant and easier to read.

Connectez-vous pour commenter.

Réponses (1)

James Tursa
James Tursa le 11 Fév 2021
Modifié(e) : James Tursa le 11 Fév 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.
  1 commentaire
Alvaro Sepulveda
Alvaro Sepulveda le 12 Fév 2021
Hi @James Tursa thanks for your help, the problem was fix

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by