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

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Cell Elements
AI Thread Summary
To efficiently extract elements from a large cell array in MATLAB, using the syntax `a{:}(1,2)` results in an error due to multiple outputs. Instead, employing `cellfun(@(f)f(1,2), a)` allows for uniform operations across the cell array. While some users prefer traditional loops for clarity, vectorization is recommended for performance improvements. Many users acknowledge the learning curve associated with MATLAB's syntax, especially when transitioning from other programming languages like Java or FORTRAN. Overall, understanding MATLAB's unique operators can lead to significant efficiency gains in coding practices.
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
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 member 428835
Although arguably slower too since matlab's syntax allows for vectorization of operations.
 
  • Like
Likes 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 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 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 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 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 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
2
Views
3K
Replies
1
Views
3K
Replies
1
Views
1K
Replies
1
Views
5K
Replies
2
Views
25K
Replies
22
Views
2K
Replies
8
Views
2K
Back
Top