Indexing the row number for a max value in a column in a cell array, and then making a new vector populated by that same row number but in another column in that cell array.
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a cell array (P) and I am taking data out of that cell array and making a new table. The code I am using is as follows:
%% CALCULATE INSTANTANEOUS FREQUENCY
% instantaneous_fq = (fs/50) / (locs_sb(i) - locs_sb(i-1) )
%create a cell array that will store the instantaneous_fq at each event for
%each breath within each LC event
instantaneous_fq = cell(1,length(P));
metaTable = table();
%loop through each LC
for lc = 1:length(P)-1
instantaneous_fq{lc} = (fs/50) ./ (diff(P{lc}(:,1)));
% Compute the values for each row
metaTable.proms_dff(lc) = proms_dff(lc);
metaTable.widths_dff(lc) = widths_dff(lc);
metaTable.area_dff(lc) = ((proms_dff(lc)) * (widths_dff(lc)))/2;
metaTable.AVG_breath_width(lc) = mean(P{lc}(:,2));
metaTable.PEAK_breath_width(lc) = max(P{lc}(:,2));
metaTable.AVG_breath_prom(lc) = mean(P{lc}(:,3));
metaTable.PEAK_breath_prom(lc) = max(P{lc}(:,3));
metaTable.AVG_inst_fq(lc) = mean((fs/50) ./ (diff(P{lc}(:,1))));
metaTable.PEAK_inst_fq(lc) = max((fs/50) ./ (diff(P{lc}(:,1))));
metaTable.AVG_acc(lc) = mean((diff(instantaneous_fq{lc}) / (fs/50)));
metaTable.PEAK_acc(lc) = max((diff(instantaneous_fq{lc}) / (fs/50)));
end
I am trying to add in one final column of the meta.Table where I find the row number of the PEAK_breath_prom(lc) in each cell array. I then want to find the value in that same row number in the first column. I want those values in the 1st column that correspond to the row with the peak value in the PEAK_breath_prom(lc) to make my final column in the cell array. What is the best way to go about indexing that row number and then finding the number in that same row in column 1, throughout the cell array to then populate the final metaTable columm?
One cell array looks like this for example:
6 6.2052 1.8595
15 3.1307 2.5363
25 3.8442 1.6293
36 3.9015 2.6188
47 4.8597 2.0607
58 6.3597 1.6547
73 5.0544 1.6657
So my PEAK_breath_prom(lc) for this cell array would be 2.6188. (as seen by the code up top and pasted again here, the peak prom value is taken from the 3rd column: metaTable.PEAK_breath_prom(lc) = max(P{lc}(:,3)).
This max value is in the 4th row. So I then want to go to the 4th row in the first column, for ex 36 in this case. I would like to do that for each cell array in the entire, larger cell array (P), just as I did to find that PEAK_breath_prom(lc) value and add it to the table.
댓글 수: 0
채택된 답변
Voss
2024년 1월 3일
편집: Voss
2024년 1월 3일
You can use the second output of max(), which tells you the index of the (first instance of the) maximum value.
Here I'm storing that index as variable idx, and then storing the value from P{lc}, row idx column 1, as "PEAK_row" in metaTable. You can change that name to what you like.
%% CALCULATE INSTANTANEOUS FREQUENCY
% instantaneous_fq = (fs/50) / (locs_sb(i) - locs_sb(i-1) )
%create a cell array that will store the instantaneous_fq at each event for
%each breath within each LC event
instantaneous_fq = cell(1,length(P));
metaTable = table();
%loop through each LC
for lc = 1:length(P)-1
instantaneous_fq{lc} = (fs/50) ./ (diff(P{lc}(:,1)));
% Compute the values for each row
metaTable.proms_dff(lc) = proms_dff(lc);
metaTable.widths_dff(lc) = widths_dff(lc);
metaTable.area_dff(lc) = ((proms_dff(lc)) * (widths_dff(lc)))/2;
metaTable.AVG_breath_width(lc) = mean(P{lc}(:,2));
metaTable.PEAK_breath_width(lc) = max(P{lc}(:,2));
metaTable.AVG_breath_prom(lc) = mean(P{lc}(:,3));
[metaTable.PEAK_breath_prom(lc),idx] = max(P{lc}(:,3));
metaTable.AVG_inst_fq(lc) = mean((fs/50) ./ (diff(P{lc}(:,1))));
metaTable.PEAK_inst_fq(lc) = max((fs/50) ./ (diff(P{lc}(:,1))));
metaTable.AVG_acc(lc) = mean((diff(instantaneous_fq{lc}) / (fs/50)));
metaTable.PEAK_acc(lc) = max((diff(instantaneous_fq{lc}) / (fs/50)));
metaTable.PEAK_row(lc) = P{lc}(idx,1);
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!