필터 지우기
필터 지우기

Find a vector of string in a Cell array

조회 수: 1 (최근 30일)
Mojgan
Mojgan 2013년 4월 29일
Hi all
Assume I have a cell array which is like a matrix like this Dataset=
{'DICL','Coating','Yes';
'DICL','Coating','No';
'DICL','Coating','Yes';
'DICL','Coating','NO'}
Now I want to find the index of a row of this array which is for example
uniquevalue={'DICL','Coating','NO'}
uniquevalue is another cell array which is like a 1*n array
When I wanted to convert the Dataset to Matrix with cell2mat,following error happened:
Error using cat: Dimensions of matrices being concatenated are not consistent.
Actually I wanted to convert these to cell arrays to the matrix and then use following statement
ind=strfind((NewstringsDataSet),(UniqueValuesSet(j,:)));
or
temp=find(strcmp(NewstringsDataSet,UniqueValuesSet(j,:)));
Are there any function in Matlab to find the index of a row of cell array
Best Regards Mojgan
  댓글 수: 1
Jan
Jan 2013년 4월 29일
Please post a complete copy of the error message and at least the line, which causes the error. Otherwise we cannot suggest an improvement for the relevant part of the code, but have to invent the algorithm from scratch. This decreases the chance, that our suggestion match your needs.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 4월 29일
편집: Andrei Bobrov 2013년 4월 29일
[~,i1] = ismember(Dataset,uniquevalue);
index = find(all(diff(i1,1,2)==1,2));
other variant:
Dataset = {
'DICL' 'Coating' 'Yes'
'DICL' 'Coating' 'No'
'DICL' 'Coating' 'YES'
'DICL' 'Coating' 'NO'};
uniquevalue = {'DICL' 'Coating' 'YES'
'DICL' 'Coating' 'NO'};
[~,~,c] = unique([Dataset;uniquevalue]);
s = size(Dataset);
M = reshape(c,[],s(2));
[ii,index] = ismember(M(1:s(1),:),M(s(1)+1:end,:),'rows');
out = [find(ii),index(ii)]
  댓글 수: 2
Mojgan
Mojgan 2013년 5월 1일
Thanks a lot
I needed the index of all uniquevalue in Dataset and your answer worked correctly
Mojgan
Mojgan 2013년 5월 2일
편집: Jan 2013년 5월 2일
Hi again
What if both Dataset and uniquevalue contain both strings and numerics? like:
Dataset = {
'DICL' 'Coating' 'Yes' 10
'DICL' 'Coating' 'No' 12
'DICL' 'Coating' 'YES' 11
'DICL' 'Coating' 'NO' 13};
uniquevalue = {'DICL' 'Coating' 'YES' 11
'DICL' 'Coating' 'NO' 13};
When I wrote [ii,index] = ismember(M(1:s(1),:),M(s(1)+1:end,:),'rows'); in such situation, I got following error:
Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string.
[EDITED, Jan, code formatted - please do this by your own, Thanks]

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2013년 4월 29일
Data = {'DICL','Coating','Yes'; ...
'DICL','Coating','No'; ...
'DICL','Coating','Yes'; ...
'DICL','Coating','NO'};
Search = {'DICL','Coating','NO'};
index = strcmp(Data(:, 1), Search(:, 1)) & ...
strcmp(Data(:, 2), Search(:, 2)) & ...
strcmp(Data(:, 3), Search(:, 3));
Or perhaps you want find(index).
  댓글 수: 2
Mojgan
Mojgan 2013년 5월 1일
Thanks Jan
I needed to find Index and I used the Andrei answer.It worked
Jan
Jan 2013년 5월 2일
편집: Jan 2013년 5월 2일
If you need the index instead of the logical index, you can append this line:
index = find(index)
Then, I think, my solution is much more direct. ismember and unique both performs an expensive sorting, but for large inout (perhaps 100'000 rows) this supports a much faster binary search of matching strings.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by