Comparing 2 lists and assign a value
4 views (last 30 days)
Show older comments
Hi,
I would like to compare 2 lists. One(A) is 1D(3 x1) table and other(B) is a 2D one(3x 2). I would like to search the prsence of each element of B in A. If any element of B(B(i)) presents in A I would assign value, B(j) against a cell array of size A. I have solved it using procedural way. But to me, the complexity of the problem is a big issue when the size of A and size of B will be really big. I believe, there must have a solution which replaces this procedural way
Input:
A{10 x 1} table
=A{1 x1}='I am a boy'
=A{2 x 1}='I am good'
=A{3 x 1}='I will be best'
B{3 x 2} table
=B{1 X 1}='bad', B{1 X 2}=0.5
=B{2 X 1}='good', B{2 x 2}=0.3
=B{3 X 1}='boy', B{3 X 2}=0.3
The program will look up each element of A for the presence of any element of B. I have solved this in the following way:
s1 is the size of A
s1_ is the size of B
for i=1:s1
for j=1:s1_
if(strcmp(B.Var1(j),A(i)))
store(i)=B.Var2(j);
break;
end
end
end
But this solutiuon will no be a good one when the s1 and s1_ becomes too large. Would you have any idea how to solve it any simpler and fast way?
thanks,
0 Comments
Answers (1)
Rik
on 9 Sep 2019
If you can make two arrays, you can use the ismember function, which should work well for large arrays as well.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!