Column Number Matching

1 回表示 (過去 30 日間)
charles atlas
charles atlas 2012 年 6 月 20 日
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 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 20 日
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2012 年 6 月 20 日
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 件のコメント
charles atlas
charles atlas 2012 年 6 月 21 日
That worked perfectly, thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by