Create a matrix on the basis of other matrix

1 回表示 (過去 30 日間)
luca
luca 2019 年 10 月 14 日
回答済み: Andrei Bobrov 2019 年 10 月 14 日
Hi given a matrix
M= [ 0 4 8 6;
0 0 0 6;
0 0 0 6;
2 0 0 0;
0 0 0 0;
0 0 0 1;
0 4 7 0;
5 3 0 0;
0 0 0 6;
0 0 0 6;];
I want to create a new matrix A that contain just the diversity, no matter of the order and with no zeros.
so in this case
A = [2 4 8 6;
5 3 7 1];
Someone can help me with the code?

採用された回答

Fabio Freschi
Fabio Freschi 2019 年 10 月 14 日
編集済み: Fabio Freschi 2019 年 10 月 14 日
Under the assumption that the "diversity" contains the same number of entries each row
for i = 1:size(M,2)
b(:,i) = unique(nonzeros(M(:,i)));
end
If each column can have different number of interest
b = arrayfun(@(i)unique(nonzeros(M(:,i))),1:size(M,2),'UniformOutput',false)
then you can access to the unique values of the jth row as
b{j}

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2019 年 10 月 14 日
[~,j] = find(M);
C = accumarray(j,M(M ~= 0),[],@(x){unique(x,'stable')})
if all(diff(cellfun(@numel,C)) == 0)
C = [C{:}];
end

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by