필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
balandong
balandong 2017년 7월 8일
편집: balandong 2017년 7월 8일
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
balandong
balandong 2017년 7월 8일
편집: balandong 2017년 7월 8일
Hi Jan,
Thanks for the code, it work perfectly.
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