필터 지우기
필터 지우기

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

조회 수: 13 (최근 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.

채택된 답변

Christopher Berry
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
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.
Pinga
Pinga 2014년 8월 11일
편집: Pinga 2014년 8월 11일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by