Find index of value extracted from subset in larger set

3 views (last 30 days)
DavidL88
DavidL88 on 8 Jan 2022
Commented: DavidL88 on 8 Jan 2022
I have a dataset in the form of a struct with two fields - each field is a double with 1,500 x 1 values. One field is t values, the other is p values. These are values are matched across rows. I want to find the minimum p value and then the corresponding t value within multiple subsets across these 1,000 values. For example, the minimum p value between rows 325:400 e.g. p value 0.04 in row 357 and the correspond t value which will then be in row 357 in the t values field. I can find the minimum p value within a subset (rows 325:400) but I don't know how to then get the index of this p value within the larger dataset so I can extract the corresponding t value. The below code gives me the index of the p value within the subset only i.e. row 32 (357-325=32) . I can try using find as below, but there are mutlple datapoints in the larger set that will match this p value. Is there a way to get the index within the larger dataset?
p = min(pvalue(325:400));
[idx1 idx2] = find(p == (pvalue));
t = tvalue(idx1)

Accepted Answer

Stephen23
Stephen23 on 8 Jan 2022
Edited: Stephen23 on 8 Jan 2022
The simple MATLAB approach is to use indexing:
X = 325:400;
[p,Y] = min(pvalue(X));
t = tvalue(X(Y));

More Answers (1)

_
_ on 8 Jan 2022
start_row = 325;
end_row = 400;
[min_p,min_idx] = min(pvalue(start_row:end_row));
min_idx = min_idx+start_row-1;
t = tvalue(min_idx);

Tags

Community Treasure Hunt

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

Start Hunting!