Clustering matrices in cells

Hello.
Given a cell of square matrices of the same size M={M{1},..., M{N}} I would like to group them in K(<N) cells G={G{1},...,G{K}} in this way:
If (conditions(i,j)) M{j} goes to group G{i} end
So far I wrote this code:
If (conditions(i,j)) G{i}={G{i}, M{j}} end
But it does not work as I expected, just merges the matrices inside grup G_i, and I want group i to be something like G{i}={M{i_1},..., M{i_s}}.
Any idea about how to do such a task?
Thank you in advance!

回答 (1 件)

Jos (10584)
Jos (10584) 2017 年 12 月 6 日

0 投票

M = {1 2 3 4} % N=4, 1-by-1 matrices for simplicity
conditions = logical([1 1 0 1; 0 1 1 0]) % K = 2
G = arrayfun(@(k) M(conditions(k,:)),1:size(conditions,1),'un',0)

1 件のコメント

Maicol Ochoa
Maicol Ochoa 2017 年 12 月 9 日
Hello Jos, thank you very much for your answer. I forgot to mention that my conditions are prefixed in a for loop, I mean: they are not arbitrary conditions. However your idea seems to be helpfull. Actually I've tried something like it with real numbers and realized that it works perfectly for 1-by-1 matrices but not for larger matrices (not even for the 2-by-2 case). If you come up with any other idea, that'd be great! Thanks again!

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2017 年 12 月 6 日

コメント済み:

2017 年 12 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by