Store data into new variable based on condition

10 visualizaciones (últimos 30 días)
BenL
BenL el 1 de Feb. de 2017
Comentada: dpb el 2 de Feb. de 2017
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

Respuesta aceptada

dpb
dpb el 1 de Feb. de 2017
Editada: dpb el 1 de Feb. de 2017
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 comentarios
BenL
BenL el 2 de Feb. de 2017
thank you, but how does Step 2 actually work?
dpb
dpb el 2 de Feb. de 2017
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"

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Functions en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by