how to output logical arrays in each cell element of the cell array using ismember function???

4 views (last 30 days)
So for example, lets say I have two cell arrays A and B. Each cell element contains a column vector. I would like to compare each cell element(which is a column vector of values) between A and B using ismember function.
A = {[], [], [], [], [6;15]; 81, [20;66], [81;162;189], 91, [6;8;9;15;20;49;69;136;163;179;189]};
B = {[], [], [], [], 15; [24;128], [], [81;72], [89;134;152], [6;15;30;34;36;53;80;124]};
So I tried using ismember function to output a logical array in each cell element using double for loop:
for i = 1:2 %rows
for j = 1:5 %columns
[C(i,j)] = ismember(A{i,j},B{i,j});
%I also tried the following to get a cell array of logical arrays.
C{i,j} = ismember(A{i,j},B{i,j});
end
end
But it's giving me errors such as: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts." Or "Cell contents assignment to a non-cell array object." I would like C to output something like this:
C = {0, 0, 0, 0, [0;1]; 0, [0;0], [1;0;0], 0, [1;0;0;1;0;0;0;0;0;0]}
But it doesn't seem to work?? Eventually, I would like to count the number of ones using sum() or find() for each cell element and put that value back into the same 2x5 format either in a cell array or numerical array.
I hope that makes sense. If not I can explain. Thank you!

Accepted Answer

dpb
dpb on 3 Jan 2016
Use cellfun to apply functions to each cell or comparably-sized cell arrays--
>> cellfun(@ismember,A,B,'uni',0)
ans =
[] [] [] [] [ 2x1 logical]
[0] [2x1 logical] [3x1 logical] [0] [11x1 logical]
>>

More Answers (0)

Categories

Find more on Cell Arrays 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!