Applying functions to each cell in a cell array

Good afternoon,
I have a 60 by 1 cell array that stores matrices of different sizes (all numeric). I want to perform certain functions (mean, diff, hypot, convhull...etc) on these matrices from the 1st element of the cell array, to the 60th element.
Is there a way I can do that?
Thank you.

댓글 수: 3

Thank you all for your responses. So suppose I have the following array, consisting of x and y coordinates of various points
c1=[1,2; 3,4; 5,6]; % 3 x-coordinates and 3 y-coordinates
c2=[2,10; 4,8; 9,3; 10,2; 4,5]; % 5 x and y coordinates
c3=[3,8; 9,1; 7,10; 5,1]; % 4 x and y coordinates
C={c1; c2; c3};
is there a way I can calculate the area of the convhull for each cell of the array?
Jan
Jan 2017년 8월 4일
Did you read the answers, which have been given already?
Yes I did Jan. But ran into errors. Trying to fix them now.

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

 채택된 답변

the cyclist
the cyclist 2017년 8월 4일
편집: the cyclist 2017년 8월 4일

1 개 추천

cellfun does exactly this.

추가 답변 (2개)

Moses Wayne
Moses Wayne 2017년 8월 4일

0 개 추천

The function call "cellfun" is a useful function in this case. In the case of the function mean, the code below would work to get the mean of each subset within the cell array:
myCell = {[0 1 2]; [3 4 5]; [6 7 8]; [9 10 11]}
result = cellfun(@mean,myCell)
In this case, result would be an array of values [1 4 7 10]. I've linked the documentation to "cellfun" here.
Adam
Adam 2017년 8월 4일

0 개 추천

If you have multiple functions to perform a for loop would likely be simplest. The idea that for loops are very slow in Matlab is often erroneous. I favour cellfun and arrayfun from an aesthetic perspective, but from a speed perspective for loops are usually faster. And certainly if you want to calculate multiple things then a single pass over the data in a for loop is a lot better than multiple calls to cellfun, though you could of course create a composite function that will calculate all your results on a single cell and then call this via cellfun if you prefer the cellfun approach.

카테고리

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

태그

질문:

2017년 8월 4일

댓글:

2017년 8월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by