Effacer les filtres
Effacer les filtres

How to efficiently find the index cell-string and cell-number?

2 vues (au cours des 30 derniers jours)
balandong
balandong le 8 Juil 2017
Modifié(e) : balandong le 8 Juil 2017
Dear Matlab Coder,
The idea was to find the index if each of the row fulfill the following condition, such as
Second column of ROW contain the string= Acond Third column of ROW contain the string = SS1 Seventh column of ROW contain the number = 1
Manually inspect, the index that fulfill the following condition are index 4 15 26.
To automated the procedure, the following code is realize. The mat file is attached together in this question
load ('raw.mat')
index1 = find((strcmp(raw (:,2), 'ACond') & (strcmp(raw (:,3), 'SS1'))));
index2 = find([raw{:,7}] == 1);
As notice in the above line, I had to used two index. I believe this the last two line can be combined to get a compact representation. So, the following code is proposed
load ('raw.mat')
index1 = find((strcmp(raw (:,2), 'ACond') & (strcmp(raw (:,3), 'SS1')) & ([raw{:,7}] == 1)));
However, the proposed code is not working as intended. May I know the workaround for this problem?.
To add, may I know a more compact representation if the condition are to be extended, say
index1 = find( (strcmp(raw (:,1), 'Sub3') & (strcmp(raw (:,2), 'ACond') & (strcmp(raw (:,3), 'SS1'))...
& ([raw{:,5}] == 4)) & ([raw{:,7}] == 1)));
Thanks in advance for the time entertaining this problem

Réponse acceptée

Jan
Jan le 8 Juil 2017
Modifié(e) : Jan le 8 Juil 2017
This does not work:
index2 = find([raw{:,7}] == 1);
The first element raw{1,1} is the string 'Trial'. Therefore [raw{:,7}] is a 1 x 44 char vector, but you want the numerical values only.
Crop the first and the last row at first:
FileData = load('raw.mat');
raw = FileData.raw(2:end-1, :);
index = find(strcmp(raw(:,2), 'ACond') & ...
strcmp(raw(:,3), 'SS1') & ...
[raw{:,7}].' == 1) + 1;
If you do not now, which rows are valid in advance:
FileData = load('raw.mat');
raw = FileData.raw;
valid = find(cellfun('isclass', raw(:, 2), 'char') & ...
cellfun('isclass', raw(:, 3), 'char') & ...
cellfun('isclass', raw(:, 7), 'double'));
index = valid(strcmp(raw(valid,2), 'ACond') & ...
strcmp(raw(valid,3), 'SS1') & ...
([raw{valid,7}].' == 1));
  2 commentaires
balandong
balandong le 8 Juil 2017
Modifié(e) : balandong le 8 Juil 2017
Hi Jan,
Thanks for the code, it work perfectly.
Jan
Jan le 8 Juil 2017
You are welcome. I'm glad that I can help you. Personally, I like to keep religion and sharing of solutions for Matlab problems separated.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by