How can I get a first value of each index from the other column?

1 回表示 (過去 30 日間)
Jaehwi Bong
Jaehwi Bong 2019 年 9 月 24 日
コメント済み: Rik 2019 年 9 月 24 日
I have this kind of data. First column and seconds column refer to an index and x value relatively.
data = [1 200; 1 201; 1 202; 1 201; 2 301; 2 313; 2 311; 3 401; 3 452; 3 433; 3 405; 4 504; 4 303; 4 604; 4 703; 5 600; 5 700; 5 606; 5 703; 5 905; 5 444;];
I want to have first value of 2nd colum of each index.
I'd like to have this kind of data that has half length (or tird whatever ratio) of index.
data = [1 200; 2 301;3 401;4 604;5 600;];
If anyone can help, it would be greatly appreciated.
Thank you!

採用された回答

Rik
Rik 2019 年 9 月 24 日
Use unique to get the list of first indices, the use either find or min in a loop.
  2 件のコメント
Jaehwi Bong
Jaehwi Bong 2019 年 9 月 24 日
編集済み: Jaehwi Bong 2019 年 9 月 24 日
Thank you for your answer!
After I use the unique function, I'd like to get the corresponding values of second colum to the first value of first column. I don't want to use min.
for i = 1: max(col1)
col2(col1==i); %how can I get the 1st value of every iteration col2?
end
Could you give me advice again,please?
Rik
Rik 2019 年 9 月 24 日
This doesn't exactly match your output, but I suspect that could be a typo.
data = [1 200; 1 201; 1 202; 1 201; 2 301; 2 313; 2 311; 3 401; 3 452; 3 433; 3 405; 4 504; 4 303; 4 604; 4 703; 5 600; 5 700; 5 606; 5 703; 5 905; 5 444;];
col1=unique(data(:,1));
output=col1;
for n=1:numel(col1)
output(n,2)=data(find(data(:,1)==col1(n),1),2);
end
clc,disp(output)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by