MATLAB Extract elements from a cell

1,766
69
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?
 
10,937
4,448
Heres a Matlab reference on brace notation for data access


it seems you are doing it wrong from the reference example.

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.
 

Wrichik Basu

Insights Author
Gold Member
2018 Award
1,135
974
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.
 
1,766
69
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.
 
10,937
4,448
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.
 
1,766
69
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:
 
33
35
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)
 

Dr Transport

Science Advisor
Gold Member
2,224
390
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++.
 
1,766
69
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!
 

Want to reply to this thread?

"Extract elements from a cell" You must log in or register to reply here.

Related Threads for: Extract elements from a cell

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top