How to find indices of elements but keep the order unchanged?

34 次查看(过去 30 天)
Hi all,
I have a cell array ch_names of channel names and I would like to get indexes of certain channels that I have given in pickCh. I have done this as follows:
Idx = find(ismember(ch_names,pickCh));
However, this gives the indexes in increasing order. Is there any better way than using a loop to keep the order of the indexes as it is in pickCh?
Thank you already in advance!
Regards, Maria

采纳的回答

Roger Stafford
Roger Stafford 2014-9-6
[tf,loc] = ismember(ch_names,pickCh);
[~,p] = sort(loc(tf));
idx = find(tf);
idx = idx(p);
(This assumes this are no repetitions in 'pickCh'. Otherwise there is ambiguity as to the proper ordering.)
  1 个评论
ibrahim Salim
ibrahim Salim 2018-2-8
Hello, I used this code but I'v changed the first line with : [tf,loc] = cellfun(@(subc) ismember(pickCh, subc,'rows'), ch_names); to seek about a specific row. This line was slow. could you please help me to speed up run time by using the same command or another one.
Thanks

请先登录,再进行评论。

更多回答(1 个)

Maria
Maria 2014-9-6
Thanks a lot! This seems to work.
-Maria

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by