Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

NCHOOSEK - What am I doing wrong?

Asked by Simon

Simon (view profile)

on 24 May 2013
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 >.<

0 Comments

Simon

Simon (view profile)

Products

No products are associated with this question.

5 Answers

Answer by Image Analyst

Image Analyst (view profile)

on 25 May 2013
Accepted answer

Sure, if you want to regress, just run this demo (where I made up some arbitrary values):

CRY = rand(1, 20);
DXY = rand(1, 20);
GOLDS = rand(1, 20);
USCRWTIC = rand(1, 20);
USGG2YR = rand(1, 20);
USGG10YR = rand(1, 20);
V = nchoosek({CRY,DXY,GOLDS,USCRWTIC,USGG2YR,USGG10YR},4)
for i= 1:15
    A{i} = V(i,:);
end
y = sin(1:20);
% Regress y against A{1}
% Get one cell array of 4 cells from the first element of A
caRowOne = A{1}
% Extract just the first of those 4 cells.
xTraining = caRowOne{1}
% Plot training data.
plot(xTraining, y, 'bo');
hold on;
% Do a fit to a line.
coefficients = polyfit(xTraining, y, 1);
% See how well we did.
x = linspace(min(xTraining(:)), max(xTraining(:)), 40);
fittedY = polyval(coefficients, x);
plot(x, fittedY, 'r-', 'LineWidth', 2);
grid on;
title('Y vs. A{1}', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);

1 Comment

Simon

Simon (view profile)

on 26 May 2013

Pretty cool stuff Brah! I will try to loop this for all 15 combinations and then a p-value, F and t-test for each. Thanks a lot!

Image Analyst

Image Analyst (view profile)

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 24 May 2013
Edited by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 24 May 2013
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 Comments

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 24 May 2013

Why do you want to create A1,A2 and A3, while one variable A is enough ?

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 24 May 2013

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 Simon

Jan Simon (view profile)

on 25 May 2013

@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.

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 24 May 2013
A = num2cell(V,2);

0 Comments

Andrei Bobrov

Andrei Bobrov (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 25 May 2013

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 Comments

Simon

Simon (view profile)

on 25 May 2013

Will i be able to perform regression on each cell of A(i) with another variable? (cell 1,1 with Y , then Cell 1,2 with Y , etc)

Jan Simon

Jan Simon (view profile)

on 25 May 2013

@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.

Jan Simon

Jan Simon (view profile)

Answer by Simon

Simon (view profile)

on 26 May 2013

Thanks everyone! If could accept everyone's answer i would..

3 Comments

Randy Souza

Randy Souza (view profile)

on 31 May 2013

Simon, you can always vote for the other answers!

Simon

Simon (view profile)

on 31 May 2013

How?

Randy Souza

Randy Souza (view profile)

on 31 May 2013

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"

Simon

Simon (view profile)

Contact us