Store data into new variable based on condition
조회 수: 7 (최근 30일)
이전 댓글 표시
I have a n X 10 dataset (variable name = dataset, see attached file). Of interest are column 1 (variable name T) and column 6 (variable name E). What I want to do is this:
1) Compare column 6 values in each cell to a threshold variable (variable name thresh). Identify the cells which are greater than this threshold (i.e. E(:,1) > thresh)
2) For the rows which fulfill condition 1, note the corresponding value in column 1 and find all rows in the dataset which have exactly the same values in column 1 (these are typically a few before or after the row which fulfill condition 1).
3) Copy these rows from condition 2 and store them into new variable data_1.
I have tried this code:
A=max(size(dataset));
T(:,1) = data(:,1);
E(:,1) = data(:,6);
thresh = 85;
for i = 1:A
if E(i,1)> thresh
[row, ~]=find(T(:,1)==T(i,1));
row_size = max(size(row));
for j = 1
data_1(j:j+row_size-1,1:10) = data(min(row):max(row),1:10);
end
j=1+row_size;
end
end
However the answers are wrong. The result in data_1 is a 5x10 variable with incorrect values. Can someone please advice what is going wrong?
Thank you. Regards Ben
댓글 수: 1
Stephen23
2017년 2월 1일
Splitting data into a variable named data_1 is not a good sign. This might be relevant:
채택된 답변
dpb
2017년 2월 1일
편집: dpb
2017년 2월 1일
Matlab Tutorial (not your problem, but worth noting in general)
A=max(size(dataset)); --> length(dataset)
which will only be number of rows if there are more rows than column. If you want number of rows for sure, then use
nR=size(dataset,1);
For your problem
Step 1:
ix=dataset(:,6)>thresh; % logical addressing vector of values above threshold
Step 2:
data=dataset(ismember(dataset(:,1),dataset(ix,1)),:); % match col 1 values in above set to rest of population
댓글 수: 2
dpb
2017년 2월 2일
You've got the subset of the total that satisfy the condition on column 6 as the group selected via the logical indexing array. Then, you wanted to match any of the values in column 1 that were included in that group so ismember returns another logical indexing array of position in the entire dataset, column 1 that are matched by the values in the subset, column 1.
Read
doc ismember % and examples to see "how it works"
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!