Column Number Matching

1 vue (au cours des 30 derniers jours)
charles atlas
charles atlas le 20 Juin 2012
I have two columns of data stored in array "A" one is random numeric data and the other is just numbers 1, 2, 3, 4, or 5. I want to look at A and take any row that has ones in the second column and store it in array R1, take any row that has twos in the second column and store it in array R2, take any row that has threes in the second column and store it in array R3, take any row that has fours in the second column and store it in array R4, and finally I want to take any row that has fives in the second column and store that data in an array "R5"
Thanks, Charles
  1 commentaire
Walter Roberson
Walter Roberson le 20 Juin 2012
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 20 Juin 2012
As long as you only have a few variables, such as 5, then you can do it simply in one line of code per number (variable):
m = randi(5, [10 2]) % Create sample data.
% Get the second column so we can check its values.
secondColumn = m(:,2)
% Create R1 through R5
R1 = m(secondColumn==1, 1)
R2 = m(secondColumn==2, 1)
R3 = m(secondColumn==3, 1)
R4 = m(secondColumn==4, 1)
R5 = m(secondColumn==5, 1)
  1 commentaire
charles atlas
charles atlas le 21 Juin 2012
That worked perfectly, thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by