Extracting multiple points from matrix and plotting

8 ビュー (過去 30 日間)
Faizan Tahir
Faizan Tahir 2016 年 2 月 19 日
コメント済み: Star Strider 2016 年 2 月 19 日
Hi.
I have a 900 row by 3 column matrix, each corresponding to a centroid of 3 objects. The 3 objects are red, green and blue.
The first column corresponds to the X coordinate, the second corresponds to Y coordinate of the objects centroid, and the 3rd column corresponds to the centroid's colour (1 = red, 2 = green, 3 = blue).
How can I efficiently loop through the matrix and filter all the rows where colour = 1, and plot all the coordinates from top to bottom, and same for the other two? I know this should be easily possible using find, but my matlab isn't great so any help is much appreciated.

回答 (1 件)

Star Strider
Star Strider 2016 年 2 月 19 日
I would use the sortrows function. It should be able to do what you want.
  1 件のコメント
Star Strider
Star Strider 2016 年 2 月 19 日
The best way to do that is with a cell array, to avoid creating dynamic variables (a poor programming practise). Here, ‘Mtx{1}’ has all the rows where the third column is 1, and so for the others. The rows remain the same.
The code:
M = [randi(99, 15, 2) randi(3, 15, 1)]; % Create Data
for k1 = 1:3
Mtx{k1} = M(M(:,3) == k1,:); % Create Cell Array
end

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

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by