MATLAB: Matlab Error Message

matrixprogramming

I have the below matlab code, and I got this error "Subscripted assignment dimension mismatch"
for s=1:6
x(:,s) = Cr(1,s) .* x2(:,s);
end
Cr is 1×6, x2 is 100×6?
Does anyone know where is the problem?

Best Answer

Your x must have strictly more or less than 100 rows. Hence the assignment fails.
Note that a much simpler way of achieving what you want is with bsxfun:
x = bsxfun(@times, Cr, x2) %that's all that's needed. No loop.