Divide an array in n different arrays

1 回表示 (過去 30 日間)
luca
luca 2019 年 9 月 17 日
編集済み: Bruno Luong 2019 年 9 月 17 日
Hi, given the array
V = [1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 5 5 5 5 5 5 5 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 1 1 1 1 1 6 6 6 6 6 6 6]
and the matrix
A = [1 3 4
6 5 2
0 8 0
0 9 0]
I want to obtain 5 different arrays
a = [1 1 1 1 1 1 6 6 6 6 6 6 6 6 6];
b = [ 3 3 3 3 3 3 3 5 5 5 5 5 5 5 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9];
c = [4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 ];
d = [1 1 1 1 1 6 6 6 6 6 6 6];
so every time I meet the elements of one of the columns: 1-6 , 3-5-8-9 or 4-2 , I have to collect them and reported in a new array.
N.B zeros are never present in V, but are just inserted in A to create a square matrix
May someone help me with this task?

採用された回答

Bruno Luong
Bruno Luong 2019 年 9 月 17 日
編集済み: Bruno Luong 2019 年 9 月 17 日
V = [1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 5 5 5 5 5 5 5 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 1 1 1 1 1 6 6 6 6 6 6 6]
A = [1 3 4
6 5 2
0 8 0
0 9 0]
[~,c,a] = find(A);
[~,i] = ismember(V,a);
g = c(i);
d = find([true; diff(g); true]);
c = mat2cell(V,1,diff(d));
c{:}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by