Extracting a matrix element which is within a cell containing cells.
Show older comments
Hi everybody.
I have a cell contaning cells, within each inner cell there is a matrix. By the way the aforementioned cell is attached. Now the question is that i want to extract the maximum value of a desired element of matrices. I have written a code, but does not seem to be an efficient one. does any can help me to make it more vectorized?
Thanks for your attention.
clc, clear, close all
% Loading mat file.
load('Sample.mat')
% Desired row and column.
Row = 5;
Col = 7;
% Preallocation.
Extracted = cell(length(Sample), 1);
Extracted(:) = {cell(12, 1)};
for yr = 1:length(Sample)
for m = 1:12
Extracted{yr,1}{m,1} = Sample{yr}{m}(Row,Col);
end
end
% Finally converting cell to multidimensional array.
Extracted = cell2mat(cat(3,Extracted{:}));
% Finding max of all pages.
FinalOutput = max(max(Extracted, [], 3));
3 Comments
Rik
on 16 Mar 2021
Did you run the profiler on this to see where it spends most time?
Two notes: always load to a struct (see the documentation for examples), that way it is always clear where variables come from. Secondly, you don't need clc,clear, close all. That is like rebooting your computer every time you run your code.
omid zandi
on 16 Mar 2021
Rik
on 16 Mar 2021
If you click on 'Problem' that will take you to a breakdown of the runtime of that function where you can see which lines take the most time.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!