sort matrix according to parity?

1 回表示 (過去 30 日間)
Tiberius
Tiberius 2014 年 2 月 22 日
コメント済み: Tiberius 2014 年 2 月 22 日
I have a matrix with many rows and 4 columns, and in one of the columns it's the parity (that is, only single values of 0 or 1). Can I sort this matrix to have the rows disposed according to parity, but alternating? That is, let's say you have this (third column is parity) a=[409 20 1 124; 324 11 1 135; 123 33 0 122; 11 12 0 124] and I want to obtain this: a=[11 12 0 124; 324 11 1 135; 123 33 0 122; 409 20 1 124]. (let's say I want to sort it also by first column, as a second criterion).
Does anyone has any nice idea for this? Thanks

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 2 月 22 日
z = sortrows(a,1);
out = zeros(size(a));
out(1:2:end,:) = z(z(:,3)==0,:);
out(2:2:end,:) = z(z(:,3)==1,:);
  1 件のコメント
Tiberius
Tiberius 2014 年 2 月 22 日
Thank you very much.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by