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!