Finding the maximum two values within one field conditioned on the values in another field

1 visualización (últimos 30 días)
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 comentarios
Kristin
Kristin el 20 de Jun. de 2022
Editada: Kristin el 20 de Jun. de 2022
%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 :)

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 20 de Jun. de 2022
% 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

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by