MATLAB Answers

Group values of a vector into new vectors according to magnitude

조회 수: 3(최근 30일)
Lu Da Silva
Lu Da Silva 2021년 9월 17일
답변: Jan 2021년 9월 17일
I have a vector A with different values of angles:
A = [17 323 100 3 278 220 45 351 212 51]
what I need is to group all angles between 0 and 50° into a new vector called B, all angles between 50° and 100° into a new vector called C, and all angles between 100° and 360° into a new vector called D.
How can I achieve that?

채택된 답변

Chunru
Chunru 2021년 9월 17일
A = [17 323 100 3 278 220 45 351 212 51]
A = 1×10
17 323 100 3 278 220 45 351 212 51
B = A(A>=0 & A<50)
B = 1×3
17 3 45
C= A(A>=50 & A<100)
C = 51
% Leave D for you

추가 답변(1개)

Jan
Jan 2021년 9월 17일
A = [17 323 100 3 278 220 45 351 212 51];
Y = discretize(A, [0, 50, 100, 360])
Y = 1×10
1 3 3 1 3 3 1 3 3 2
C = splitapply(@(x) {x}, A, Y)
C = 1×3 cell array
{[17 3 45]} {[51]} {[323 100 278 220 351 212]}

Community Treasure Hunt

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

Start Hunting!

Translated by