필터 지우기
필터 지우기

how to group data points in matrix

조회 수: 1 (최근 30일)
Amy Xu
Amy Xu 2017년 4월 27일
편집: Andrei Bobrov 2017년 4월 27일
Matrix A as follows:
A = [
10
20
5
15
35
40
25
30
5
10
];
Based on the following criteria, I want to give code to every array (X) in matrix A:
if 0 < X <= 5 then code = 1
if 5 < X <= 10 then code = 2
if 10 < X <= 15 then code = 3
if 15 < X <= 20 then code = 4
if 20 < X <= 25 then code = 5
if 25 < X <= 30 then code = 6
if 30 < X <= 35 then code = 7
if 30 < X <= 40 then code = 8
results something like:
out = [
10 2
20 4
5 1
15 3
35 7
40 8
25 5
30 6
5 1
10 2
];

채택된 답변

Roger Stafford
Roger Stafford 2017년 4월 27일
Assuming all numbers in X lie between 0 and 40, and that X is a column vector as indicated in your description, then do:
code = sum(bsxfun(@le,X,5:5:40),2);

추가 답변 (2개)

Don Mathis
Don Mathis 2017년 4월 27일
out = [A floor(A/5)]

Andrei Bobrov
Andrei Bobrov 2017년 4월 27일
편집: Andrei Bobrov 2017년 4월 27일
out = [A,discretize(A,0:5:max(A)+1,'IncludedEdge','right')];

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by