How to select specific rows with a certain range/certain value in another row?

25 vues (au cours des 30 derniers jours)
Two simple questions I haven't managed to find the right answers for:
1: I have a 1x1 cell called "Data". This 1x1 cell contains a 1x100 cell, so basically the 100th data would be Data{1,1}{1,100}. In the analysis I only want to include the data until cell 90. I've tried it with
Data = Data{1,1}{1,1:90}
which doesn't seem to work.
2: I have a 10x8 double. What I'd like to calculate is the mean value of all the values in row 2 for all data which value in their row 4 is "5".
Any help would be appreciated.

Réponse acceptée

Christopher Berry
Christopher Berry le 11 Août 2014
Pinga,
1. To get Data as a 1x90 cell array, use the following code:
Data = Data{1}(1:90)
There are two ways to refer to the elements of a cell array. Enclose indices in smooth parentheses, (), to refer to sets of cells—for example, to define a subset of the array. Enclose indices in curly braces, {}, to refer to the text, numbers, or other data within individual cells.
2. Assume that your 10x8 double data is in A
mean(A(2,A(4,:)==5));
Here, A(4,:)==5 returns a logical vector of each column containing a '5' in row 4, which you then use to logically index into row 2 columns.
Hope that helps.
  3 commentaires
Christopher Berry
Christopher Berry le 11 Août 2014
Ok, I think I understand what you are asking, but if I get it wrong let me know.
1. What you want sounds like
Data = {Data{1,1}(1:90)}
This way your size of data is 1x1 cell, whose 1st cell contents are a 1x90 cell array.
2. When you say "I meant columns, not rows", I am going to guess your question should have been "What I'd like to calculate is the mean value of all the values in column 2 for all data which value in their corresponding row column 4 is "5".
And it looks like that is what you are doing, but with misplaced (). Try this instead
mean(A(A(:,4)==5,2))
Notice the lack of ) after "5" and the double )) at the end of the line.
Pinga
Pinga le 11 Août 2014
Modifié(e) : Pinga le 11 Août 2014
Super, that was a really great help - thanks a lot! Although having worked with Matlab for a few months now, it appears to me, I'm just adapting way too slowly on this whole new programming stuff (such misplaced () keep me troubleshooting and frustrating quite some time).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by