I am trying to find vectorized matlab function ind = item2ind(item,t) to solve following problem: I have a list of row vectors
item = [2 3 1; 2 1 2; 3 1 1; 1 3 3]
and vector of all possible item elements at each item row vector
I need to find indexes of of separate item rows elements corresponding to the vector t in this way:
ind = [3 6 1; 3 1 4; 6 1 2; 1 6 7]
But
does not correspond to the vector t, because there are 3 "1" elements, and t contains only 2 "1" elements.
Note: Serial version is inefficient for large item (10000 x 100) and t (1 x 200).
function ind = item2ind(item,t)
[nlp,N] = size(item);
ind = zeros(nlp,N);
for i = 1:nlp
auxitem = item(i,:);
auxt = t;
for j = 1:N
I = find(auxitem(j) == auxt,1,'first');
if ~isempty(I)
auxt(I) = 0;
ind(i,j) = I;
else
error('Incompatible content of item and t.');
end
end
end
end
Additional remarks:
Most of the time is spent on the line:
I = find(auxitem(j) == auxt,1,'first');
Is there any clever trick how to speed up this line of code? I tried this, for example, but without any speedup:
I = ipos(auxitem(j) == auxt); I = I(1);
where ipos is preallocated as:
Thanks in advance for any help ...
6 Comments
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566073
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566073
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566077
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566077
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566081
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566081
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566089
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566089
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566178
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566178
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566196
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/399901-find-indices-of-row-subsets#comment_566196
Sign in to comment.