필터 지우기
필터 지우기

Divide an array in n different arrays

조회 수: 5 (최근 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 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