Why is operator = is clever sometimes but not always?

M = zeros(N,6);
M(:,1) = ones(N,1); % OK
M(:,2) = ones(1,N); % OK
M(:,[3,4]) = zeros(N,2); % OK
M(:,[5,6]) = zeros(2,N); % ERROR

Answers (2)

And why shouldn't it give an error? You're trying to stuff a 2 row by N column matrix into an N row by 2 column space. The sizes of the rows and columns don't match up. You can't stuff a n-by-m matrix into an j-by-k space unless n equals j and m equals k (unless the matrix is a 1D vector).
Vectors are treated differently as a convenience because most of the time row vector vs column vector is irrelevant to people.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 16 Jun 2015

Answered:

on 16 Jun 2015

Community Treasure Hunt

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

Start Hunting!