Divide an array in n different arrays

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by