Does Matlab resize vectors automatically
12 views (last 30 days)
Hi to everyone. I was wondering if MATLAB is able to automatically resize the arrays. To me it sounds that it does because this:
dot_p(i,:) = R(:,:,i)*uContrF(:,i);
dot_p(i,:) = (R(:,:,i)*uContrF(:,i))';
For me work both fine, even if the second one should be the most correct one. I say that because I have dot_p(i,:) which is a 1x3 then I have R(:,:,i) which is a 3x3 and then uContrF(:,i) which is a 3x1. So the result should be a 3x1 vector but because dot_p(i,:) is 1x3 the result is returned as a 1x3 vector.
What do you think ? Can you help me?
Thorsten on 1 Jul 2015
Edited: Thorsten on 1 Jul 2015
That works because the : is matched here to a row or a column vector and converted to a row vector. So you can assign either a row or a column vector, and both are stored as row vectors:
vec_row = ones(1,100);
vec_col = ones(100,1);
A(1,:) = vec_row; % works
A(2,:) = vec_col; % works
vec = A(2,:);
Name Size Bytes Class Attributes
vec 1x100 800 double
dpb on 1 Jul 2015
Edited: dpb on 1 Jul 2015
Yes, Matlab automagically reallocates storage on assignment any and every time it needs to. This includes, obviously, the changing of shape. The requirements are that
- any subscript notation on the LHS must match the output size of the RHS, and
- the arrays must be consonant in size for the operations requested on the RHS
It is a feature of a weakly typed and rapid development platform which a very generic description of Matlab.
That you specified a single row and indefinite number of columns in the LHS addressing expression "tells" Matlab you specifically require a row vector; otherwise the addressing expression would be incorrect for a column vector (of other than length one, anyway, unless i is a vector).
If you wish for the result to have the shape of the RHS irrespective of whether that is a row or column vector, then you would write simply
dot_pi = RHS;
excepting in your case where you're obviously operating in a loop and need to fill it, you must reference a location or the LHS will simply be the single vector overwritten each pass.
Hence, if you do want the the final result to be stored by column instead of row, you needs must write it that way as
dot_pi(:,i) = RHS;
instead. Matlab, being an obedient servant, simply follows your instructions. Being as noted above a rapid development system its philosophy is that as long as the rules on size and addressing can be accomodated without actual syntax or size error it'll silently do what you say on the presumption that'll get you to the end result more rapidly than having to worry about explicit details excessively as is common in more strongly typed and compiled languages where allocation isn't dynamic. The drawback is that many things that "work" thus may not be what you actually intend. "There is no free lunch!"
You don't say what you really need/want for the end result specifically, but if it's an issue that you have row-oriented result instead of column, swap the addressing index as above (or transpose the end result when the loop is done with .').