MATLAB: Adding zero column to matrix

matrixmatrix manipulation

Hi All,
Currently I'm facing an error with a Matlab code.
In my problem I have Two matrix v and stateHit
v =
0.0044 0.1620 0.0272 0.0029 0.1610 0.6035 0.0151 0.0239
0.0044 0.1639 0.0275 0.0029 0.1606 0.6019 0.0150 0.0238
0.0043 0.1657 0.0279 0.0030 0.1601 0.6002 0.0150 0.0238
stateHit =
1 0 0 0
1 0 0 0
1 0 0 0
I want to do
difMatrix= v(1:length(stateHit),:) – cpustateHit;
??? Error using ==> minus Matrix dimensions must agree.
I want add zero columns to stateHit.
Could please give an example how can add zero columns to stateHit so that matrix dimension of v and stateHit became the same and I do that operations
difMatrix= v(1:length(stateHit),:) – cpustateHit;
many thanks in advance
SM

Best Answer

Hi,
if it's just that you want to enlarge "stateHit", you could do
if size(stateHit,2)<size(v, 2)
% v has more columns
stateHit(1, size(v,2)) = 0;
end
Titus