Setting multiple variables to empty vectors

19 ビュー (過去 30 日間)
Samuel Newall
Samuel Newall 2018 年 11 月 20 日
コメント済み: Guillaume 2018 年 11 月 21 日
I want to create multiple vectors as columns of zeros so that I can then fill them in later. What I've done so far is, for example:
flowm=zeros(12,1);
flowmax=zeros(12,1);
flowmin=zeros(12,1);
But this seems a bit tedious. I attempted the following to see if it would work
[flowm,flowmax,flowmin]=zeros(12,3)
but to no avail.
Can anyone suggest what might work?
  2 件のコメント
Luna
Luna 2018 年 11 月 20 日
What do you want to create? A matrix 12x3 or after filling the columns you want to merge them?
Samuel Newall
Samuel Newall 2018 年 11 月 20 日
I just want to set the vectors flowm, flowmax etc to columns of zeros in a compact way. What i am trying to do is similar to below:
imagine I had the matrix (1 2 3; 4 5 6; 7 8 9)
I want to be able to equate the vector matrix [a, b, c] (this is the matrix where the vector a is the first column, the vector b is the second, and vector c is the third) to the above matrix, such that the value of a is now (1;4;7), b is (2;5;8) and c is (3;6;9) or in matlab terms, a=[1 4 7]', b=[2 5 8]', c=[3 6 9]'
I hope that makes sense.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2018 年 11 月 20 日
編集済み: Stephen23 2018 年 11 月 20 日
[flowm,flowmax,flowmin]= deal(zeros(12,1))
  9 件のコメント
Stephen23
Stephen23 2018 年 11 月 21 日
編集済み: Stephen23 2018 年 11 月 21 日
@Guialluame: sure, it is clear why this allocation has to occur at some point. But you have not explained why this will be slower overall than allocating one hundred matrices at the start of the code.
As far as I can see, the time would be more or less the the same:
tic
create 100 matrices
do stuff with them
toc
vs.
tic
create one matrix
do stuff that creates 99 matrices
toc
As long as only 100 matrices are created, I don't see why "The 100 lines of code would be marginally faster as deal will cause 99 extra copy operations to occur at some point." It is clear that they have to be created at some point, but why would this matrix creation be "marginally faster" at the start of the code? Surely the matrix creation mechanism is not slower at the end of my script than at the start, or at some random point while it is running... Or are you simply excluding the matrix creation at the start of the code from your comparison?:
create 100 matrices
tic
do stuff
toc
vs.
create 1 matrix
tic
do stuff that creates 99 matrices
toc
Guillaume
Guillaume 2018 年 11 月 21 日
My point was more that due to c-o-w mechanism the actual matrix creation is delayed to a time where it may not be expected. If the actual creating occurs in a loop which is expected to run at a fixed frequency, c-o-w may break that expectation.
In addition, in the second case, there are 99 copy operations that do not occur in the first case. However, I must admit that I forgot about the 99 setting the memory to 0 that occur in the first case. So the comparison is in effect (C equivalent):
%100 x zeros is
100 x malloc %allocate memory
100 x memset %set blocks to 0
vs
%1 x zeros
1 x malloc
1 x memset
%99 x copy-on-write:
99 x malloc
99 x memcpy
I would suspect that memset is generally faster than memcpy, the former is just writing to memory, the latter is both reading and writing. So I would expect the first version to be faster overall.
This discussion probably doesn't matter for the OP though, and a much more important point that I made has been lost:
I wouldn't recommend having 100 of variables anyway. Particularly, if they're all the same size, I would greatly recommend you have one single table instead (where the 100 variables are variables of the table).

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by