How to add vector from cell?

3 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2015-2-4
编辑:Guillaume 2015-2-4
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 2015-2-5
编辑:Stephen23 2015-2-5
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.

请先登录,再进行评论。

采纳的回答

Michael Haderlein
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 个评论
Machine Learning
Machine Learning 2015-2-4
编辑:Machine Learning 2015-2-4
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 2015-2-4
编辑:Stephen23 2015-2-4
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by