Does Matlab resize vectors automatically

12 views (last 30 days)
chef13 on 1 Jul 2015
Edited: Muthu Annamalai on 1 Jul 2015
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);
and this:
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?
  1 Comment
Muthu Annamalai
Muthu Annamalai on 1 Jul 2015
Edited: Muthu Annamalai on 1 Jul 2015
Please note,
  1. operator ' represents a Hermitian conjugate
  2. operator .' is mostly what you want to use for plain vanilla transpose

Sign in to comment.

Answers (2)

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,:);
whos vec
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
  1. any subscript notation on the LHS must match the output size of the RHS, and
  2. 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 .').
dpb on 1 Jul 2015
Edited: dpb on 1 Jul 2015
"Strong" or "weak" typing is a feature of programming languages that refers to what level of consonance one must include manually. See Wikipedia on the subject:
An example in Matlab of weak typing and what it means and allows would be something like
>> whos x
Name Size Bytes Class Attributes
x 1x1 132 struct
>> x=rand(1,3);
>> whos x
Name Size Bytes Class Attributes
x 1x3 24 double
Note that variable x is first a structure from a previous unshown assignment which is followed by an assignment to x of an "ordinary" default double vector. Silently the previous x has been destroyed and a new variable of class double created. In a typed language, the assignment would have triggered either a compilation error or runtime error in an interpreted language.
This is part of the "rapid development" paradigm owing to the fact you don't have to have previously declared x as a given type nor explicitly destroy it or any such thing. As noted, that saves some effort in writing code at the price of possibly making an error that isn't caught until later on in that maybe you really did not intend to lose the information that was in the structure. That tradeoff between how easy or simple it is to write something versus what is or isn't caught out for you as a possible error by the toolset is why I referred to the old saw of "no free lunch"--the other language might actually have saved more than you gained in the particular instance if the cost of generating the structure were high. OTOH, presumably you're using a RAD environment owing to things like graphics and/or the myriad of additional functions and all that are builtin to it that would have had to either develop or find from other sources that overall probably do shorten time to an answer.
"Rapid development platform" is the integration of all the higher-level features that together tend to make coding a much faster process to get to an end result as compared to writing everything with a "traditional" compile/link/test toolset such as C or even C++. Refer to the TMW web page for highlights of what's included with base Matlab and think of doing much of that starting with just a compiler. Matlab features

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!