필터 지우기
필터 지우기

How to bin struct fields based on value in another field?

조회 수: 3 (최근 30일)
Nicholas Kavouris
Nicholas Kavouris 2023년 1월 22일
답변: Walter Roberson 2023년 1월 22일
I have a structure with multiple fields. I would like to plot a histogram of values of data in one field based on conditions in another field of each row of the struct. How would i do this?
EX:
test(1).initial=13
test(1).final=43
test(2).initial=8
test(2).final=58
test(3).initial=[26,14]
test(3).final=52
i would like to bin test.final so rows with corresponding test.initial(1) values 0-10, 11-20, 20-35 are grouped together
  댓글 수: 3
Nicholas Kavouris
Nicholas Kavouris 2023년 1월 22일
i only want to bin based on test.initial(1), any index beyond 1 is ignored
Nicholas Kavouris
Nicholas Kavouris 2023년 1월 22일
i added this in as the struct i am working with has a variable number of indices in each row in the column id like to sort by

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 1월 22일
initials = arrayfun(@(S) S.initial(1), test);
finals = [test.final].'; %assuming only one value per entry
bins = discretize(initials, [0 11 21 35]);
splitapply(@histogram, finals, bins);
legend({'[0-11)', '[11-21)', '[21-35]'});
Your bin list included 20 twice, and does not include the range between 10 and 11 (exclusive), so I had to guess what you wanted.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by