Store data into new variable based on condition

10 ビュー (過去 30 日間)
BenL
BenL 2017 年 2 月 1 日
コメント済み: dpb 2017 年 2 月 2 日
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

採用された回答

dpb
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 件のコメント
BenL
BenL 2017 年 2 月 2 日
thank you, but how does Step 2 actually work?
dpb
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 ExchangeFunctions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by