Finding the maximum two values within one field conditioned on the values in another field
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I have a structure with 8 rows and 11 fields. The data contained in the 8 rows (corresponding to summary data of 8 blocks) belongs to different conditions of an experiment (rows 1,2,5,6 belong to condition A and rows 3,4,7,8 belong to condition B). I would like to extract the index (from 1 to 8) of the two maximum values within each condition.
I am currently struggeling with writing a code that extracts the maximum values only from the rows of the relevant condition and at the same time returns the number of the block/row that it refers to.
So I would, for example, like to get the maximum values of condition B, which are in my case in row/block 4 and 7. How do I do this?
Would be so grateful for an answer!!!
댓글 수: 2
채택된 답변
Voss
2022년 6월 20일
% random data
feedback = struct('gains',num2cell(rand(1,8)));
disp([feedback.gains]);
%separately save gains from Condition A and Condition B
idxA = [1 2 5 6];
idxB = [3 4 7 8];
gains_total = [feedback.gains];
condA_data = gains_total(idxA);
condB_data = gains_total(idxB);
%sort gains from free and forced choice blocks in descending order
[sorted_condA,sorted_idxA] = sort(condA_data,'descend');
[sorted_condB,sorted_idxB] = sort(condB_data,'descend');
%find the index of the two best free and forced choice blocks
top2_condA_blocks = idxA(sorted_idxA([1 2]))
top2_condB_blocks = idxB(sorted_idxB([1 2]))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!