Vector to Indicate if Data equals the Maximum by group

조회 수: 1 (최근 30일)
Qize Chen
Qize Chen 2022년 4월 19일
답변: Akira Agata 2022년 4월 19일
I have a data vector and a group vector, how do I generate a vector (the same size as the data vector) of 0's and 1's to indicate if data is the group maximum. For example, data = [1;2;3;6;5] and group = [1;1;2;3;3]. I want to get [0;1;1;1;0].

채택된 답변

Akira Agata
Akira Agata 2022년 4월 19일
How about the following?
data = [1;2;3;6;5];
group = [1;1;2;3;3];
idx = splitapply(@(x) {x == max(x)},data,group);
idx = cell2mat(idx);
disp(idx)
0 1 1 1 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by