Column Number Matching

1 visualizzazione (ultimi 30 giorni)
charles atlas
charles atlas il 20 Giu 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 Commento
Walter Roberson
Walter Roberson il 20 Giu 2012
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 20 Giu 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 Commento
charles atlas
charles atlas il 21 Giu 2012
That worked perfectly, thank you.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by