필터 지우기
필터 지우기

Extracting a matrix element which is within a cell containing cells.

조회 수: 2 (최근 30일)
omid zandi
omid zandi 2021년 3월 16일
댓글: omid zandi 2021년 3월 16일
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
omid zandi
omid zandi 2021년 3월 16일
First: I run the profiler and attached the result screen shot, but can't get much out of it. How can it help? The point is that sample.mat in future can become much larger than what i have loaded right now. so later this code will put me in trouble.
Second: Thanks for your comments about loading to structure and not using clc, clear at the start of the code.
Rik
Rik 2021년 3월 16일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2021년 3월 16일
편집: Stephen23 2021년 3월 16일
Putting scalar numeric data into nested cell arrays is pointlessly complex and inefficient. Get rid of the cell arrays:
S = load('Sample.mat');
% Desired row and column.
Row = 5;
Col = 7;
% Preallocation of one numeric matrix:
N = numel(S.Sample);
M = nan(N,12);
for yr = 1:N
for m = 1:12
M(yr,m) = S.Sample{yr}{m}(Row,Col);
end
end
max(M(:))
ans = 63
The storage of the original data would also be improved by not using nested cell arrays.
  댓글 수: 1
omid zandi
omid zandi 2021년 3월 16일
Thanks. It is faster than my code.
Although nested cell arrays are a beat confusing and inefficient, they make sense to me, and they are a very helpful in iterative operations.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by