how to find cell array elements in other arrays

15 visualizzazioni (ultimi 30 giorni)
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 Commenti
Guillaume
Guillaume il 20 Gen 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.
mhm
mhm il 20 Gen 2016
Modificato: mhm il 20 Gen 2016
sorry that I make this mistake.you are right. and thanks for your mention.the second type is correct.I am new in matlab. So what is my answer?

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 20 Gen 2016
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'};
X = cellfun(@(c)ismember(source,c),{bb,cc,dd,ee},'UniformOutput',false);
Y = sum(vertcat(X{:}),1)
output:
Y =
3 0 1 0 4 3
  3 Commenti
Guillaume
Guillaume il 20 Gen 2016
" I'd like use matlab facilities and tricks for this problem in order better performance and optimization code"
More efficient code is a worthwhile goal. Unfortunately, this is not always achieved by avoiding loops altogether (but it often is). In terms of performance, the above is probably slower than the loop version. In terms of memory, it uses more.
You have the cost of an anonymous function call, which in matlab has never been cheap. You also have the cost of creating an output cell array and then converting the cell array to a matrix. These two operations are not performed by the loop version.
The only advantage of the cellfun is better clarity of the intent of the code (apply the same function to all elements of the array).
mhm
mhm il 21 Gen 2016
Dear my friend...
Thank you for your mentions and guides.

Accedi per commentare.

Più risposte (2)

Image Analyst
Image Analyst il 19 Gen 2016
Take a look at the ismember() function.

Guillaume
Guillaume il 20 Gen 2016
Modificato: Guillaume il 20 Gen 2016
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 Commenti
mhm
mhm il 20 Gen 2016
Modificato: mhm il 20 Gen 2016
We can use ismember function and we should use for loops but I don't like use it. I'd like use matlab facilities and tricks for this problem in order better performance and optimization code.
how can i combine ismember and cellfun for this problem?
Guillaume
Guillaume il 20 Gen 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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by