Get array of elements based on occurrences of another vector

조회 수: 1 (최근 30일)
Enrico Gambini
Enrico Gambini 2022년 9월 9일
답변: Matt J 2022년 9월 9일
Hi guys!
Let's assume that you have 2 Nx1 arrays "y" and "x", is there a way to find all the elements of y corresponding to the unique elements of x?
E.g
y=[0;1;2;3;4];
x=[1;1;2;2;2];
%The results should be two vectors, one containing the elements [0;1] and the other one containing
%[2;3;4]
Thank you!

채택된 답변

Torsten
Torsten 2022년 9월 9일
y=[0;1;2;3;4];
x=[1;1;2;2;2];
z{1} = y(x==1);
z{2} = y(x==2);
z{1}
ans = 2×1
0 1
z{2}
ans = 3×1
2 3 4

추가 답변 (1개)

Matt J
Matt J 2022년 9월 9일
y=[0;1;2;3;4];
x=[1;1;2;2;2];
z=splitapply(@(x){x},y,x);
z{:}
ans = 2×1
0 1
ans = 3×1
2 3 4

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by