Create a concatenated matrix in matlab

조회 수: 2 (최근 30일)
Alvaro Sepulveda
Alvaro Sepulveda 2021년 2월 11일
댓글: Jan 2021년 2월 12일
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
Jan
Jan 2021년 2월 12일
Note: 1*10.^-7 is a mutliplication and an expensive power operation. 1e-7 is a cheap constant and easier to read.

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

답변 (1개)

James Tursa
James Tursa 2021년 2월 11일
편집: James Tursa 2021년 2월 11일
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
Alvaro Sepulveda
Alvaro Sepulveda 2021년 2월 12일
Hi @James Tursa thanks for your help, the problem was fix

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

카테고리

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