Explanation of cellfun() (2024)

301 views (last 30 days)

Show older comments

Marvin Eckert on 20 May 2020

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun

Commented: Marvin Eckert on 25 Nov 2021

Accepted Answer: Rik

Open in MATLAB Online

Explaination of cellfun() call with @(data):

Hi everyone,

I have a rather stupid question I think, but I do not understand a specific call of the cellfun().

My question came up when I was working with the MATLAB example: Similarity-Based Remaining Useful Life Estimation. There is a function applied to each cell of a cell array, for example in line 34:

trainDataNormalized = cellfun(@(data) regimeNormalization(data, centers, centerstats), ...

trainData, 'UniformOutput', false);

[...] line 176: (for info)

function data = regimeNormalization(data, centers, centerstats)

For me the content of the cellfun() and its function is clear, exept of the expression @(data). Cellfun() applies the function regimeNormalization individual to each cell of the cell array trainData.

Looking in the doku of cellfun() they call a funktion like this and leave out the additional function like above. Which I think I undestand, see below.

A = cellfun(@mean,C)

p = cellfun(@plot,X,Y);

But in the documentation they also do this, which is exactly like my problem, but the explaination is not sufficient for me. (MATLAB advanced beginner) What is the expression @(x) mean?

B = cellfun(@(x) x(1:3),str,'UniformOutput',false)

Does cellfun() accessing one cell of the cell array temporarily store the data inside the cell in the variable x?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Rik on 20 May 2020

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#answer_434183

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#answer_434183

Open in MATLAB Online

cellfun will apply your function to the contents of you cell array. There are 3 ways to specify the function:

  1. as a char array (only a handfull of functions, but they are faster when called like this)\
  2. with a function handle, e.g. @mean
  3. with an anonymous function

That last option is what you see here.

%instead of this

function output=MyFun(in1,in2)

output=in1.*in2;

end

%you do

MyFun=@(in1,in2) in1.*in2;

4 Comments

Show 2 older commentsHide 2 older comments

Marvin Eckert on 20 May 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_851678

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_851678

Edited: Marvin Eckert on 20 May 2020

Open in MATLAB Online

Tanks for your quick answer. :)

Your answer were helpflull, because I did not thought about anonymous functions. I think I undestand now what the cellfun()

trainDataNormalized = cellfun(@(data) regimeNormalization(data, centers, centerstats), ...

trainData, 'UniformOutput', false);

above is doing.

To be shure I explain it in my words and you give a short YES or NO? That would be very helpful to me.

The function temporarily stores the information within a single cell of the trainData cell array in the variable data, executes the regimeNormalization() function using the variable data as input, and writes the result of regimeNormalization() function back to this cell.

John Navarro on 24 Nov 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1850669

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1850669

Hello. I think the answer is NO. cellfun is required because of the format or the variable, or the data type. So far that I understand is that regimenNormalization() is being evaluated for each cell of the variable (data).

Chech this for more details

https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html

https://www.mathworks.com/help/matlab/ref/cellfun.html?s_tid=doc_ta

Rik on 24 Nov 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1850679

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1850679

Open in MATLAB Online

Actually the answer is yes.

Cellfun in this case is only hiding the loop.

%the first step is to create an anonymous function with the input variable

%called data. The values of the variables centers and centerstats are

%captured with their current value.

anon_function=@(data) regimeNormalization(data, centers, centerstats);

%the second step is to execute the anonymous function for each cell and

%store the result in a cell array.

trainDataNormalized = cellfun(anon_function, ...

trainData, 'UniformOutput', false);

The cellfun call is equivalent to this:

trainDataNormalized=cell(size(trainData));

for n=1:numel(trainData)

trainDataNormalized{n}=anon_function(trainData{n});

end

The loop will probably be marginally faster.

It is the anonymous function that is evaluated for every cell. Inside that anonymous function there is a call to regimenNormalization, which is called with the data in each cell, but also with two other variables that were captured when the anonymous function was created.

Marvin Eckert on 25 Nov 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1852714

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1852714

Hi Rik,

very cool. Thanks for your comment much appreciated.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsMatrices and ArraysMatrix Indexing

Find more on Matrix Indexing in Help Center and File Exchange

Tags

  • cellfun
  • cellfun()

Products

  • MATLAB
  • Predictive Maintenance Toolbox

Release

R2019b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Explanation of cellfun() (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Explanation of cellfun() (2024)

FAQs

What does cellfun mean in MATLAB? ›

cellfun returns the outputs of func in cell arrays. The outputs of func can have any sizes and different data types.

Is Cellfun faster than for loop MATLAB? ›

cellfun and arrayfun are cool and allow a compact notation. But they are not designed to be faster than loops.

How to find an empty cell in MATLAB? ›

If you want to know whether the the i-th cell is empty, you have to use curly brackets to access the content of the cell. Also, ==[] evaluates to empty, instead of true / false , so you should use the command isempty instead. In short: a(i)==[] should be rewritten as isempty(a{i}) .

How to convert cell into string in MATLAB? ›

Direct link to this answer
  1. To convert a cell array of character vectors to a character array, use the “char” function.
  2. To extract the contents from a cell, index using curly braces.
  3. Starting in R2016b, you can store text in string arrays. To convert a cell array to a string array, use the “string” function.
Feb 1, 2015

How to find the number of elements in an array in MATLAB? ›

n = numel( A ) returns the number of elements, n , in array A , equivalent to prod(size(A)) .

How to read data from MATLAB? ›

Use fopen to open the file, specify the character encoding, and obtain the fileID value. When you finish reading, close the file by calling fclose(fileID) . A = fscanf( fileID , formatSpec , sizeA ) reads file data into an array, A , with dimensions, sizeA , and positions the file pointer after the last value read.

Which for loop is most efficient? ›

Answer: for (reverse)

It still leaves me wondering that when I tested the difference for loop statements, this (reverse) is the most effective and fastest for loop javascript of the entire for loops. Here's an example. It could entail a loop of an array of over one million items.

Which loop executes faster? ›

Performance-wise, For loops are generally faster than While loops. For Keyword is used. While Keyword is used.

What is faster than for loops? ›

Map is often faster than loops because it uses an optimized internal mechanism for iterating over the collection.

How do you check if a cell is not empty in MATLAB? ›

Description. TF = isempty( A ) returns logical 1 ( true ) if A is empty, and logical 0 ( false ) otherwise. An empty array, table, or timetable has at least one dimension with length 0, such as 0-by-0 or 0-by-5.

How do you check if a cell is NaN in MATLAB? ›

TF = isnan( A ) returns a logical array containing 1 ( true ) where the elements of A are NaN , and 0 ( false ) where they are not. If A contains complex numbers, isnan(A) contains 1 for elements with either real or imaginary part is NaN , and 0 for elements where both real and imaginary parts are not NaN .

How do I remove missing from a cell in MATLAB? ›

To remove missing entries from a structure array, apply rmmissing to each field in the structure by using the structfun function. To remove missing entries in a cell array of non-character vectors, apply rmmissing to each cell in the cell array by using the cellfun function.

How do I convert a cell to an array in Matlab? ›

A = cell2mat( C ) converts a cell array into an ordinary array. The elements of the cell array must all contain the same data type, and the resulting array is of that data type. The contents of C must support concatenation into an N-dimensional rectangle. Otherwise, the results are undefined.

What is CELL2STR in Matlab? ›

CELL2STR converts a cell array of strings into a character array. The resulting character array is possible by padding the strings in each row with spaces.

How do you combine strings in Matlab cell? ›

For a string or cell array of any size, join concatenates elements along the last dimension of str with a size that does not equal 1 . newStr = join( str , delimiter ) combines the text in str and places the elements of delimiter between the elements of str instead of a space character.

What does bsxfun mean in MATLAB? ›

The bsxfun function expands the vectors into matrices of the same size, which is an efficient way to evaluate fun for many combinations of the inputs.

What is a cell array in MATLAB? ›

Description. A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text, combinations of text and numbers, or numeric arrays of different sizes.

What does Xlim mean in MATLAB? ›

xlim( limits ) sets the x-axis limits for the current axes or chart. Specify limits as a two-element vector of the form [xmin xmax] , where xmax is greater than xmin .

What does Ezsurf mean in MATLAB? ›

Description. example. ezsurf( f ) creates a surface plot of the function f(x,y) using the surf function. The function plots f over the default interval [-2π 2π] for x and y . ezsurf automatically adds a title and axis labels to the plot.

Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 5673

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.