I have a cell array "myCell" with 2 dimensions and each cell has a 2D matrix and I'd like to obtain scalar value of the maximum for a given structure among all the cell dimensions. I assume bsxfun can do this but my goal doesn't match they way that I normally see bsxfun get used.
Currently I get the max (of the abs) looping through each cell dimension, grabbing the max for all elements in that cell's structure I need, assign the (scalar) max to elements in a matirx which shares the cell array dimensions, then tak the max of the matrix with (:) as shown below.
peak_FcRn(noFc,noRa)=0; % each cell array has structure, whose maxes I want to compare
for iFc=1:noFc
for iRa=1:noRa
% get the maximum absolute value of the current cell array's structure
% called noise (myCell{iRa,iFc}.noise--2 or 3d array)
peak_FcRn(iFc,iRa) = max(abs(myCell{iRa,iFc}.noise(:))); % a scalar value
end
end
% the
peak = max(peak_FcRn(:)); %
This gets the right answer but I would really like a one- or two-liner that can get the max of the cell's structure elements--and I have a feeling bsxfun can do it!
In the laziest way I would type
peak = max(abs(myCell{:,:}.noise(:))) % or perhaps
peak = max(abs(myCell{:}.noise(:)))
Thanks for your input,
Michael B.

댓글 수: 2

darova
darova 2020년 5월 4일
Try cellfun
Thanks for your input. I got it to work with a cute two-liner!
peakPerCELL = cellfun(@(x) max(abs(x.noise(:))),myCell);
peak = max(peakPerPADS(:));
Cheers!
Michael B.

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

 채택된 답변

James Tursa
James Tursa 2020년 5월 4일

0 개 추천

If you reshape the input first, a one-liner version of your code:
peak = max(cellfun(@(x) max(abs(x.noise(:))),myCell(:)));

댓글 수: 3

Thanks that's even neater! Can you explain why taking the max outside cellfun ignores the dimensions (i.e. doesn't need an associated "(:)"? I realize I could use
peak = max(cellfun(@(x) max(abs(x.noise(:))),myCell(1,:)));
to grab along a chosen dimension, but why is the default to treat the output of cellfun (a whos tells me it's a double) like a 1D vector?
Thanks!
James Tursa
James Tursa 2020년 5월 4일
편집: James Tursa 2020년 5월 4일
The myCell(:) reshapes myCell into a column vector and passes that into cellfun( ), which in turn creates the output with the same shape as the input ... a column vector. Since the output is now already a column vector, no need to reshape it again before taking max( ).
Michael Bowles
Michael Bowles 2020년 5월 4일
I understand and realize my question is silly now! I confused my self before posting my second question because I thought I hadn't changed the last cellfun input with (:).
Thanks for your help! I learned great stuff today!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2020년 5월 4일

댓글:

2020년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by