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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 5K views
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?
 
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
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
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!