Clear Filters
Clear Filters

Column Number Matching

1 view (last 30 days)
charles atlas
charles atlas on 20 Jun 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 Comment
Walter Roberson
Walter Roberson on 20 Jun 2012
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 20 Jun 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 Comment
charles atlas
charles atlas on 21 Jun 2012
That worked perfectly, thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!