NCHOOSEK - What am I doing wrong?
显示 更早的评论
V = nchoosek({CRY,DXY,GOLDS,USCRWTIC,USGG2YR,USGG10YR},4);
for i= 1:15;
A{i} = V(i,:);
end;
Basically, I would need to create A1 to A15 containing the 'i' line of the matrix V. For example:
V = [1 2 3 4 ; 5 6 7 8 ; 9 0 1 2]
I would need
A1 = [1 2 3 4]
A2 = [5 6 7 8]
A3 = [9 0 1 2]
I've been searching for this for all day >.<
采纳的回答
更多回答(4 个)
Azzi Abdelmalek
2013-5-24
编辑:Azzi Abdelmalek
2013-5-24
V = [1 2 3 4 5 6 7 8 9 0 1 2];
V=reshape(V,4,[]);
for i= 1:3
A{i} = V(:,i)';
end
6 个评论
Simon
2013-5-24
Azzi Abdelmalek
2013-5-24
Your variables are
A{1}
A{2}
A{3}
Simon
2013-5-24
Azzi Abdelmalek
2013-5-24
Why do you want to create A1,A2 and A3, while one variable A is enough ?
Azzi Abdelmalek
2013-5-24
If you insist to create A1,A2 and A3 (which is not recomanded)
V = [1 2 3 4 5 6 7 8 9 0 1 2];
V=reshape(V,4,[]);
for i= 1:3
eval([ sprintf('A%d',i) '= V(:,i)'''])
end
Jan
2013-5-25
@Simon: Don't use EVAL. Creating A1, A2, ... is a bad programming pattern and therefore the most cited question from the FAQ: FAQ: How can I create A1, A2, ... in a loop .
Staying with the original matrix and using V(i, :) would be much more direct and efficient instead of inserting indirect intermediate copies to a bunch of variables, which require crude tricks to be access automatically.
Andrei Bobrov
2013-5-24
A = num2cell(V,2);
Jan
2013-5-25
1 个投票
V(i, :) is perfect already. Are you sure that there is any reason for adding a level of abstraction by splitting the matrix to a bunch of vectors?
2 个评论
Simon
2013-5-25
Jan
2013-5-25
@Simon: It is very likely, that you can perform the regression even with the original matrix directly. The most Matlab commands can handle a matrix input and process the operations for the subvectors automatically. When you post the code you want to apply, more detailed suggestions are possible.
When you want to access the contents of a cell, use the curly braces: A{1} etc.
Simon
2013-5-26
0 个投票
3 个评论
Randy Souza
2013-5-31
Simon, you can always vote for the other answers!
Simon
2013-5-31
编辑:Randy Souza
2013-5-31
Randy Souza
2013-5-31
There should be a triangle with "x Votes" under the profile picture for the answerer. Click that and it should change to "(x + 1) Votes"
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!