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

3 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2017-7-8
编辑:Jan 2017-7-8
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 个评论
Jan
Jan 2017-7-8
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by