how to find cell array elements in other arrays
Show older comments
Hi everybody
I have some cell array like this:
source = {'a','b','c','d','e','f'}
bb = {'a','e','g','l','f','h'}
cc = {'e','c','j','k','l','aa'}
dd = {'a','z','x','yy','e','f','a','a'}
ee = {'z','h','e','a','aa','f','e','a'}
I wana search each element in source trough other arrays. for example for first element of source I expect to have 3. because there are 'a' in bb,dd,ee.
best regards.
7 Comments
Walter Roberson
on 19 Jan 2016
What should be done if there are multiple places that a occurs within any of those? Should the count be only 1 per cell no matter how many times it occurs in the cell, or should the count be 1 per occurrence?
mhm
on 20 Jan 2016
Walter Roberson
on 20 Jan 2016
Are the elements in source guaranteed to be numeric scalars? Are they guaranteed to be strings? Either of those two can be done efficiently; if they can be other data types then the comparisons are more expensive.
If any of the values are permitted to be structures, then do you want the comparison to proceed matching field to field? Or do you want the comparison to fail if there is a structure which has the same fields but in a different order?
In the case of numeric values, if the values exist but in a different numeric class, then do you want the test to succeed or does the numeric class need to be the same? For example, should uint8(42) be considered to match 42.0 ? Should 0 be considered to match logical false? Should 1 be considered to match logical true? Should any non-zero non-nan value be considered to match logical true?
Guillaume
on 20 Jan 2016
It is extremely important to use valid matlab syntax in your question to avoid confusion.
c = {a, b, c}
is a cell array which contains three elements, the content of variable 'a', 'b' and 'c', these could be strings, scalar numbers, matrices, more cell arrays, structures, handles, etc. Everybody read your question as the cell array can contain arbitrary data of any type.
c = {'a', 'b', 'c'}
is a cell array which contains only strings. The answer for that is much simpler.
Accepted Answer
More Answers (2)
Image Analyst
on 19 Jan 2016
0 votes
Take a look at the ismember() function.
The ismember function is your friend. To make it easier to perform the search, it is much easier to put your search arrays all in one big cell array. Giving individual names to similar variables is never a good idea.
source = {'a', 'b', 'c', 'd', 'e', 'f'}
bb = {'a', 'e', 'g', 'l', 'f', 'h'}
cc = {'e', 'c', 'j', 'k', 'l', 'aa'}
dd = {'a', 'z', 'x', 'yy', 'e', 'f'}
ee = {'z', 'h', 'e', 'a', 'aa', 'f'}
searcharrays = {bb, cc, dd, ee}; %put all search arrays into one cell array
searchcount = zeros(size(source)); %initialise count to 0
for sidx = 1:numel(searcharrays) %loop over each search array
searchcount = searchcount + ismember(source, searcharrays{sidx}); %and add 1 if source element is found
end
4 Comments
Walter Roberson
on 20 Jan 2016
This is very much the same code I was going to propose for the task until I realized I had no idea what the datatypes were. I had the minor difference of initializing the count to a scalar 0 instead of bothering with zeros(size(source))
Guillaume
on 20 Jan 2016
The code is exactly your desired result. If you look at the content of searchcount, it has the values that you want.
There are usually very good to replace loops by vectorised operations as this makes the code more efficient. This is not one of them. The loop in this case is more efficient.
@Walter, to me it makes more sense to initialise the searchcount to a vector of the right size (it degenerates better when searcharrays is empty), but indeed for the generic case, it could just be a scalar 0.
Categories
Find more on Loops and Conditional Statements 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!