0
votes

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
To me it looks like filling the array might be more appropriate than concatenating to the array since you've preallocated it using: HMC_Kh = zeros(length(Kh),2501);.MichaelTr7

1 Answers

2
votes

Cross posted on MATLAB Answers, but repeating the Answer here. 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.