vectorization of ismember function

Hello, I am interested in using the ismember function for strings by row with out using a for loop. For example I would have
A = {'cat','dog','bird'}
as my input vector and
B = {'cat','dog';'dog','bird'}
using ismember in a for loop
for k = 1:2
M(k,:) = ismember(A,b{k,:});
end
would return the matrix;
M = [1 1 0 ; 0 1 1].
Right now I have this written in a for loop but I was wondering if there is a way to do it without one. Thank you in advance.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 6월 8일
편집: Andrei Bobrov 2013년 6월 8일

0 개 추천

M = cell2mat(arrayfun(@(ii)ismember(A,B(ii,:)),(1:size(B,1))','un',0));
or
[~,b] = ismember(B,A);
M = accumarray([repmat((1:size(b,1))',size(b,2),1),b(:)],1);

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2013년 6월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by