How to add vector from cell?

2 Ansichten (letzte 30 Tage)
Machine Learning
Machine Learning am 4 Feb. 2015
Bearbeitet: Stephen23 am 5 Feb. 2015
Hi,
I have store my vector into cells and now I want to add them all up. How do I go about doing it?
for i=1:5
Y{i}=randn(10,1); %The vector I want to add
X{i}=randn(5,10); %The matrix that associate with the vector
Z{i}=X{i}*Y{i};
end
Instead of adding it one by one
total=Y{1}+Y{2}+Y{3}+Y{4}+Y{5};
Is there a faster way to do it? Because the number of loop I run can be quite big.
Or is there a better way to store the vector? I choose to store it in cell is because I have to assign matrix to the same vector as well.
Thanks in advance!
  3 Kommentare
Guillaume
Guillaume am 4 Feb. 2015
Bearbeitet: Guillaume am 4 Feb. 2015
Stephen, you say that in every single post that uses i or j as an iterator. I don't agree with it because:
a) it stands very little chance of breaking anything. Matlab uses 1i and 1j to represent the imaginary unit. i and j are just functions that return these values.
b) I'd argue that mathworks was wrong to create these functions in the first place. i and j are used as iterating variables in zillions of computer books. Matlab shouldn't hijack such a common variable name. (Matlab needs namespaces badly).
However, I'd say that in complex code, you should use variable names that explain better their purpose.
Stephen23
Stephen23 am 5 Feb. 2015
Bearbeitet: Stephen23 am 5 Feb. 2015
Good points. I had a look around and found a few references to other discussions on this. I posted a new question to try and find out what other Answers users think, and if there is some kind of consensus on how to deal with these two variable names.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Michael Haderlein
Michael Haderlein am 4 Feb. 2015
Yes:
sum(cat(3,X{:}),3)
the same for Y and Z.
Comment: I have chosen to use the third dimension as this is not existing in any cell before.
  2 Kommentare
Machine Learning
Machine Learning am 4 Feb. 2015
Bearbeitet: Machine Learning am 4 Feb. 2015
Thanks a lot. This works for me.
sum(cat(3,X{:}),3)
May I ask where can I read up more about this function sum(cat(dimension,X{:}),3) as well. I am relatively new to Matlab.
Stephen23
Stephen23 am 4 Feb. 2015
Bearbeitet: Stephen23 am 4 Feb. 2015
One of the biggest benefits to using MATLAB is the great IDE and the comprehensive help . For example, when you are in the MATLAB command window you can type the following:
doc cat
to bring up the help documentation for the function cat . You can also display the help text directly in the command window with
help sum
Tip for beginners: open the help and look at the left-hand side of the page. There you will see a very useful box entitled "Contents". You can use this to navigate around the documentation, it is great for discovering related functions, examples for particular data classes, and ton of other handy stuff that makes your life a lots easier. Spend a few minutes exploring "Contents" and get used to using it when you are writing your code. From the "Contents" you should be able to locate the page for cell-array indexing , for example.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by