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
Kristin
Kristin 2022년 6월 20일
편집: Kristin 2022년 6월 20일
%separately safe gains from Condition A and Condition B
condA_data = [feedback(1:2).gains feedback(5:6).gains];
condB_data = [feedback(3:4).gains feedback(7:8).gains];
gains_total = [feedback(1:8).gains];
%sort gains from Condition A and Condition B choice blocks in descending order
sorted_condA = sort(condA_data, 'descend');
sorted_condB = sort(condB_data,'descend');
%gain in best Condition A and Condition B blocks
top2_gains_condA = sorted_condAgains(1:2);
top2_gains_condB = sorted_condBgains(1:2);
%find the index of the two best Condition A and Condition B blocks
top2_condA_blocks = [(find(gains_total == top2_gains_condA(1))) (find(gains_total == top2_gains_condA(2)))];
top2_condB_blocks =[(find(gains_total == top2_gains_condB(1))) (find(gains_total == top2_gains_condB(2)))];
All variables are saved in the structure "feedback" and I would like to access the values of the field "gains" separately for condition A (condA) and condition B (condB).
To extract the maximum two values (maximal gains) of both conditions, I sorted the data from the field gains subsequently and saved the maximum two values in a variable called top2_gains_condA and top2_gains_condB, respectively.
Where things become problematic is the last two lines of code. What I have done so far is to create new variables and if I try to have the index of the maximum values returned, I would have indices from 1-4 for both variables. However, I would like to refer to the original indices, as saved in the field feedback.gains. This is what I tried to implement by looking for the values of the top2_gains variables in the list of the total gains (gains_total). As the gains in condA and condB sometimes overlap, however, this does not work.
In my feedback structure, I have a second field in which the blocknumber from 1-8 over row 1-8 are specified. Is there maybe an option to refer only to the values of the respective condition (A or B) by specifying the row/block by the blocknumber?
I hope my case became a bit clearer now. If you need more specification, please let me know.
Thank you already and Best regards :)

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

채택된 답변

Voss
Voss 2022년 6월 20일
% random data
feedback = struct('gains',num2cell(rand(1,8)));
disp([feedback.gains]);
0.4040 0.2714 0.5874 0.8261 0.7476 0.6085 0.9514 0.5072
%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_condA_blocks = 1×2
5 6
top2_condB_blocks = idxB(sorted_idxB([1 2]))
top2_condB_blocks = 1×2
7 4

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by