how to find cell array elements in other arrays

18 ビュー (過去 30 日間)
mhm
mhm 2016 年 1 月 19 日
コメント済み: mhm 2016 年 1 月 21 日
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 件のコメント
Guillaume
Guillaume 2016 年 1 月 20 日
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 2016 年 1 月 20 日
編集済み: mhm 2016 年 1 月 20 日
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?

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2016 年 1 月 20 日
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 件のコメント
Guillaume
Guillaume 2016 年 1 月 20 日
" 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 2016 年 1 月 21 日
Dear my friend...
Thank you for your mentions and guides.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2016 年 1 月 19 日
Take a look at the ismember() function.

Guillaume
Guillaume 2016 年 1 月 20 日
編集済み: Guillaume 2016 年 1 月 20 日
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 件のコメント
mhm
mhm 2016 年 1 月 20 日
編集済み: mhm 2016 年 1 月 20 日
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 2016 年 1 月 20 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by