How to add vector from cell?

조회 수: 1 (최근 30일)
Machine Learning
Machine Learning 2015년 2월 4일
편집: Stephen23 2015년 2월 5일
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
Michael Haderlein 2015년 2월 4일
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