How can I efficiently extract elements from a large cell in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Cell Elements
Click For Summary

Discussion Overview

The discussion revolves around extracting elements from a large cell array in MATLAB, specifically focusing on the challenges and methods for accessing data efficiently. It includes technical explanations, personal experiences with MATLAB syntax, and comparisons to other programming languages.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • One participant describes an issue with extracting a specific element from a cell array using curly brace indexing, which results in an error due to multiple outputs.
  • Another participant provides a reference to MATLAB documentation on brace notation, suggesting the original approach may be incorrect.
  • Some participants express difficulty with MATLAB's indexing syntax, preferring to use individual cell indexing or loops for clarity.
  • There is a discussion about the trade-offs between using for loops and vectorization in MATLAB, with some noting that vectorization can be faster.
  • A suggestion is made to use the `cellfun` function for applying operations across the cell array, which could streamline the extraction process.
  • Participants share personal anecdotes about their experiences with MATLAB and other programming languages, highlighting the learning curve and differences in coding styles.

Areas of Agreement / Disagreement

Participants express a mix of opinions on the best approach to data extraction in MATLAB, with no clear consensus on whether loops or vectorization is superior. Some agree on the utility of `cellfun`, while others prefer traditional indexing methods.

Contextual Notes

Some participants mention the limitations of their experience with MATLAB compared to other languages, and the challenges faced by colleagues who are less familiar with MATLAB's features. There is also mention of varying coding practices among different groups, which may affect performance.

Who May Find This Useful

This discussion may be useful for MATLAB users, particularly those dealing with large cell arrays, as well as programmers transitioning from other languages who seek to understand MATLAB's data handling capabilities.

member 428835
Hi PF!

I have a cell 1 X 5001. Each component within is a double matrix of different sizes, approximately 100 X 3. The entire cell is called a. Then when trying to extract component (1,2) from each of the 5001 matrices I type a{:}(1,2), but upon executing this MATLAB complains "Expected one output from a curly brace or dot indexing expression, but there were 5001 results." Any ideas?
 
Physics news on Phys.org
  • Like
Likes   Reactions: FactChecker, member 428835 and Wrichik Basu
jedishrfu said:
Being a java programmer, I’ve always had trouble with these slicing and dicing operations and always fall back on individual cell indexing to get my data.
I second that. I apways forget those syntaxes, whether in Matlab or in Python. Simple cell indexing using loops is always better.
 
  • Like
Likes   Reactions: member 428835
Although arguably slower too since matlab's syntax allows for vectorization of operations.
 
  • Like
Likes   Reactions: FactChecker and member 428835
Thanks for all the comments. Ended up just using a for loop, though I agree with jedishrfu statement on vectorization. Would've been nice but fortunately this computation takes about 0.1 seconds so no loss there.
 
  • Like
Likes   Reactions: Wrichik Basu
Yeah its good to know these quirky operators in MATLAB and other languages. They added to optimize and sometimes provide a cool feature to discourage migration to something else.

An expert MATLAB programmer will know these shortcuts and can whip out code faster and will laugh upon seeing the for loop stuff.
 
  • Like
Likes   Reactions: boneh3ad
jedishrfu said:
Yeah its good to know these quirky operators in MATLAB and other languages. They added to optimize and sometimes provide a cool feature to discourage migration to something else.

An expert MATLAB programmer will know these shortcuts and can whip out code faster and will laugh upon seeing the for loop stuff.
Yea no kidding. I realized a while ago the people who know their stuff well make me look like a noob...I probably am anyways :doh:
 
  • Like
Likes   Reactions: jedishrfu
For future reference, when performing a uniform operation on arrays inside of a cell, just write a function to perform your desired task and have it operate across the cell using cellfun. For your current problem it would look like this:
cellfun(@(f)f(1,2), a)
 
  • Like
Likes   Reactions: jedishrfu
joshmccraney said:
Yea no kidding. I realized a while ago the people who know their stuff well make me look like a noob...I probably am anyways :doh:

Don't beat yourself up. I work across two groups at work, my home group has zero skills outside of FORTRAN 90/95 and they don't even use all the bells and whistles. The older guys in the other group try to write in MATLAB, but it looks a lot like FORTRAN, loops all over the place and they wonder why their codes runs soooo slowwww. I grabbed on short script, vectorized it and realized a 50% time savings, only took me a week to do it. They asked another group to write them some better software for their data sets, he came back with OOP MATLAB, they were completely lost since they only had a minimal amount of C++.
 
  • Like
Likes   Reactions: member 428835 and Wrichik Basu
  • #10
Dr Transport said:
Don't beat yourself up. I work across two groups at work, my home group has zero skills outside of FORTRAN 90/95 and they don't even use all the bells and whistles. The older guys in the other group try to write in MATLAB, but it looks a lot like FORTRAN, loops all over the place and they wonder why their codes runs soooo slowwww. I grabbed on short script, vectorized it and realized a 50% time savings, only took me a week to do it. They asked another group to write them some better software for their data sets, he came back with OOP MATLAB, they were completely lost since they only had a minimal amount of C++.
Wow, even in the "real world" people still run into large speed bumps. This was shockingly very nice to read: thanks!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
43K
  • · Replies 2 ·
Replies
2
Views
25K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
13K