フィルターのクリア

assign the same vector to be the same cell

1 回表示 (過去 30 日間)
ha ha
ha ha 2018 年 11 月 21 日
コメント済み: ha ha 2018 年 11 月 24 日
Let's say, I have the matrix:
A=[x,y]=[1 2;1.1 2;1.2 2;1 3;1.1 3;1.2 3;1 4;1.1 4;1.2 4];
If i wanna group all vector having the same value of y coordinate. How can I do that?
Example, the result like that:
Cell1=[1 2
1.1 2
1.2 2]
Cell2=[1 3
1.1 3
1.2 3]
Cell3=[1 4
1.1 4
1.2 4]
  2 件のコメント
madhan ravi
madhan ravi 2018 年 11 月 23 日
People here put some efforts to help you and you mercilessly close the question without clarifying how rude
ha ha
ha ha 2018 年 11 月 24 日
@ madhan ravi . I'm very sorry. I think I misclick on the button. Very sorry for my mistake.

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

採用された回答

madhan ravi
madhan ravi 2018 年 11 月 21 日
編集済み: madhan ravi 2018 年 11 月 21 日
A=[1 2;1.1 2;1.2 2;1 3;1.1 3;1.2 3;1 4;1.1 4;1.2 4]';
group=reshape(A,2,3,3);
CELL=cell(1,3);
for i = 1:size(group,3)
CELL{i}=group(:,:,i)';
end
celldisp(CELL)
command window:
>>
CELL{1} =
1.0000 2.0000
1.1000 2.0000
1.2000 2.0000
CELL{2} =
1.0000 3.0000
1.1000 3.0000
1.2000 3.0000
CELL{3} =
1.0000 4.0000
1.1000 4.0000
1.2000 4.0000
>>
  1 件のコメント
ha ha
ha ha 2018 年 11 月 24 日
@ madhan ravi. Why did you know I have 3 cell? I think, you use number "3", because you observe there are 3 cell by visualization. But In general case, we can not know how many cell.

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

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2018 年 11 月 21 日
Cell = mat2cell(A,accumarray(findgroups(A(:,2)),1),size(A,2));

Guillaume
Guillaume 2018 年 11 月 21 日
編集済み: Guillaume 2018 年 11 月 21 日
Can be done easily with findgroups (or the older unique) and splitapply (or the older accumarray), in just one line:
A = [1 2;1.1 2;1.2 2;1 3;1.1 3;1.2 3;1 4;1.1 4;1.2 4];
C = splitapply(@(rows) {A(rows, :)}, (1:size(A, 1))', findgroups(A(:, 2)));
celldisp(C)
with unique and accumarray, you need two lines as you need the 3rd return value of unique:
A = [1 2;1.1 2;1.2 2;1 3;1.1 3;1.2 3;1 4;1.1 4;1.2 4];
[~, ~, id] = unique(A(:, 2));
C = accumarray(id, (1:size(A, 1))', [], @(rows) {A(rows, :)});
celldisp(C)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by