フィルターのクリア

How to obtain the filtered Index of Cell Array

2 ビュー (過去 30 日間)
Kushan
Kushan 2013 年 5 月 6 日
% This is the code
AllNames= {'A.1';'A.1';'A.10';'A.10';'A.10';'A.10';'A.15';'A.15';'A.16' ;'A.17';'A.17';'A.17';'A.17';'A.17';'A.20A';'A.20A';'A.20A';'A.20A'};
B=[2;6;3;4;5;1;8;6;4;3;2;8;6;1;6;8;9;2];
ide_names=unique(AllNames(1:end,1)) ;
for i= 1:length(ide_names)
index=find(AllNames(:,1)==ide_names(i,1));
data.Name(1,i)=ide_names(i,1);
data.Value=B(index,1);
end
% I want to create a new data struct(data) using the filtered Index from Cell Array A with values contain in B column vector. However, Once I ran the code below error is poping up.
_Undefined function 'eq' for input arguments of type 'cell'.
Error in a (line 6) index=find(AllNames(:,1)==ide_names(i,1));_
% could someone help me out, please
  1 件のコメント
Jan
Jan 2013 年 5 月 6 日
I have formatted your code. It is easy: mark it with the mouse, hit the "{} Code" button, care for a blank line before and after the code.

サインインしてコメントする。

回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2013 年 5 月 6 日
編集済み: Andrei Bobrov 2013 年 5 月 6 日
AllNames= {'A.1';'A.1';'A.10';'A.10';'A.10';'A.10';'A.15';'A.15';'A.16' ;'A.17';'A.17';'A.17';'A.17';'A.17';'A.20A' ;'A.20A';'A.20A';'A.20A'};
B=[2;6;3;4;5;1;8;6;4;3;2;8;6;1;6;8;9;2];
[a,c,c] = unique(AllNames); %[EDIT}
bb = accumarray(c,B,[],@(x){x});
data = struct('Name',a,'Value',bb);

Kushan
Kushan 2013 年 5 月 6 日
Thanks a lot for your quick response. But below line is not working
[a,~,c] = unique(AllNames,'stable');
The below error massage is popping up
[a,~,c] = unique(AllNames,'stable');
Error using cell/unique (line 28)
Unrecognized option.
Please help me out
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2013 年 5 月 6 日
See EDIT part in my answer.

サインインしてコメントする。


Jan
Jan 2013 年 5 月 6 日
You can change the line:
index=find(AllNames(:,1)==ide_names(i,1)); % ERROR
to
index = strcmp(AllNames(:,1), ide_names{i,1});
But using the outputs of unique is faster.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by