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

  • MATLAB
  • Thread starter member 428835
  • Start date
  • Tags
    Cell Elements
In summary, the conversation discusses a problem with extracting data from a cell in MATLAB. The individual is trying to use brace notation, but is receiving an error. Other users suggest using loops or vectorization to solve the issue. It is mentioned that an expert MATLAB programmer would know the shortcuts and be able to write code more efficiently. However, it is acknowledged that everyone has different skills and there is no need to feel like a noob. It is also mentioned that even in the real world, people can struggle with optimizing their code.
  • #1
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
  • #3
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
  • #4
Although arguably slower too since matlab's syntax allows for vectorization of operations.
 
  • Like
Likes FactChecker and member 428835
  • #5
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
  • #6
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
  • #7
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
  • #8
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
  • #9
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!
 

What is the purpose of extracting elements from a cell?

The purpose of extracting elements from a cell is to study and analyze the structure, function, and behavior of the individual components within a cell. This can provide insight into the overall function of the cell and its role in larger biological processes.

What methods are commonly used to extract elements from a cell?

Common methods for extracting elements from a cell include chemical and physical methods, such as cell fractionation, cell lysis, and chromatography. These methods allow for the separation and isolation of specific components within a cell.

What types of elements can be extracted from a cell?

Various types of elements can be extracted from a cell, including proteins, lipids, carbohydrates, nucleic acids, and organelles. These elements play important roles in the structure and function of the cell.

What are the potential challenges of extracting elements from a cell?

Some potential challenges of extracting elements from a cell include contamination of the sample, damage to the components during the extraction process, and difficulty in separating specific elements from complex mixtures.

How is the extracted material analyzed and studied?

The extracted material can be analyzed and studied using a variety of techniques, such as microscopy, spectrophotometry, and mass spectrometry. These methods allow for the identification and characterization of the extracted elements.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
22
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
43K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
13K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
204
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
Back
Top