How to select specific rows with a certain range/certain value in another row?
25 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Christopher Berry
2014-8-11
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 个评论
Christopher Berry
2014-8-11
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!