Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
compare two cell arrays

Subject: compare two cell arrays

From: Irene

Date: 14 Mar, 2011 22:41:06

Message: 1 of 9

Hello!
I have created two cell arrays A,B (1x100 cell) and i want to compare them.
Specifically, I have to check whether a value of A exists at B.
Finally, I have to create a new cell(result)(1 x 100) with similar size to A which has 1 if value of A exists in B, else 0. I'm trying this:
result=cell(1,100);
for k = 1:100
result{k}=zeros(size(A{1,k},1),1);
    for j=1:size(A{1,k},1)
    new = A{1,k};
    temp=cell(1,100);
    temp2=cell(1,100);
    temp{k}=zeros(size(A{1,k},1),1);
        for l = 1:size(B{1,k},1)
        if B{1,k}(l) <= new(j) + 30 && B{1,k}(l) >= new(j) - 30
                        temp2 = cellfun(@(x) x(x + 1), temp, 'UniformOutput',false); %%temp = temp+1;
        end
            result{1,k} = temp2;
    
        end
    end
end

The cell result is created correctly but it doesn't take any values at all. Any ideas;

Subject: compare two cell arrays

From: Jeremy

Date: 14 Mar, 2011 23:07:04

Message: 2 of 9

Hi Irene. I think something like this would work:

result = zeros(1,100);
index=1;
for i=1:size(A,2)
    for j = 1:size(B,2)
        if findstr(A{i},B{j})
            result(i) = 1;
        end
    end
end

Using this code RESULT would be a numerical vector rather than a cell array. I didn't test this code, but I think it should work.

Jeremy

Subject: compare two cell arrays

From: Irene

Date: 14 Mar, 2011 23:27:07

Message: 3 of 9

"Jeremy " <jeremy.brower@asu.edu> wrote in message <ilm72o$7qv$1@fred.mathworks.com>...
> Hi Irene. I think something like this would work:
>
> result = zeros(1,100);
> index=1;
> for i=1:size(A,2)
> for j = 1:size(B,2)
> if findstr(A{i},B{j})
> result(i) = 1;
> end
> end
> end
>
> Using this code RESULT would be a numerical vector rather than a cell array. I didn't test this code, but I think it should work.
>
> Jeremy

Hi, Jeremy
I've tried this code but it appears this error:
"Input strings must have one row".
The cells that i have do not include strings, but numbers. So is findstr useful;

Subject: compare two cell arrays

From: Jeremy

Date: 14 Mar, 2011 23:44:20

Message: 4 of 9

Hi Irene,

I'm not sure why I assumed they were strings. So are your sets of numbers (A & B) truly cell arrays? Or are they numerical arrays?

Instead of the line with findstr, try just saying:

if A{i} == B{j}

If your arrays are not cell arrays, then this should work instead:

if A(i) == B(j).

However, now that I think about it, a much more efficient method of finding which elements in A are also in B could be something like this:

results = zeros(1,100);
for i = 1:size(A,2)
 if ~isempty(setdiff(A(i),B))
  results(i) = 1;
 end
end

Hope this helps.
Jeremy

Subject: compare two cell arrays

From: Bruno Luong

Date: 15 Mar, 2011 07:02:05

Message: 5 of 9

"Irene " <eleutherio88@gmail.com> wrote in message <ilm5i2$b9$1@fred.mathworks.com>...
> Hello!
> I have created two cell arrays A,B (1x100 cell) and i want to compare them.
> Specifically, I have to check whether a value of A exists at B.
> Finally, I have to create a new cell(result)(1 x 100) with similar size to A which has 1 if value of A exists in B, else 0. I'm trying this:
> result=cell(1,100);
> for k = 1:100
> result{k}=zeros(size(A{1,k},1),1);
> for j=1:size(A{1,k},1)
> new = A{1,k};
> temp=cell(1,100);
> temp2=cell(1,100);
> temp{k}=zeros(size(A{1,k},1),1);
> for l = 1:size(B{1,k},1)
> if B{1,k}(l) <= new(j) + 30 && B{1,k}(l) >= new(j) - 30
> temp2 = cellfun(@(x) x(x + 1), temp, 'UniformOutput',false); %%temp = temp+1;
> end
> result{1,k} = temp2;
>
> end
> end
> end
>
> The cell result is created correctly but it doesn't take any values at all. Any ideas;

Please give a short example of inputs and expected output. Your cell contains obviously array of numbers, and an example will illustrate what is the goal.

Bruno

Subject: compare two cell arrays

From: Irene

Date: 15 Mar, 2011 19:58:05

Message: 6 of 9

A (cell 1X100) includes numbers

    1 2 3 ......... 100
(8X1) (10X1) (6X1) (9X1)


B(cell 1X100) includes numbers

   1 2 3 ......... 100
(71X1) (76X1) (74X1) (80X1)

So, I have to create a new cell results with the same size of A that has 1 if the value of A exists in B and 0 if it doesn't

results (cell 1X100) includes 0,1

    1 2 3 ......... 100
(8X1) (10X1) (6X1) (9X1)

I hope now i am more clear

Subject: compare two cell arrays

From: Bruno Luong

Date: 15 Mar, 2011 20:10:15

Message: 7 of 9

"Irene " <eleutherio88@gmail.com> wrote in message <ilogcd$4r8$1@ginger.mathworks.com>...

>
> I hope now i am more clear

Sill not. What you wrote above is *not* an example. What you mean by "the value of A exists in B"? Exist in *any* cell of B? Exist in the *corresponding* cell?

What do you expect the result for?

A = {[1 2 3] [4 5] [1 8] [11 12]}
B = {[4 1], [6 7 8] [9 10] [12 11]}

Bruno

Subject: compare two cell arrays

From: Irene

Date: 15 Mar, 2011 20:57:05

Message: 8 of 9

> Sill not. What you wrote above is *not* an example. What you mean by "the value of A exists in B"? Exist in *any* cell of B? Exist in the *corresponding* cell?
>
> What do you expect the result for?
>
> A = {[1 2 3] [4 5] [1 8] [11 12]}
> B = {[4 1], [6 7 8] [9 10] [12 11]}
>
> Bruno

I mean the value of A exist in the corresponding cell.

A={[923000 [938000] ...... [.....until cell 100
       936000 1098000 ....
       944000 1120000 ....
     1020000 1309000 ....]
     1234000 1456098
     1509800 1987897
     1987000 2220990
     2398000] 2340900
                          2678900
                          2798799]
B has similar form but first cell has 71 rows, second 76 rows etc like i described above.
So for example i want to take each value of the first cell of A and search if exists in first cell of B.For this i try the code i wrote in my first message.

                                                                   

Subject: compare two cell arrays

From: Bruno Luong

Date: 15 Mar, 2011 21:08:05

Message: 9 of 9

"Irene " <eleutherio88@gmail.com> wrote in message <ilojr1$fcb$1@ginger.mathworks.com>...
> > Sill not. What you wrote above is *not* an example. What you mean by "the value of A exists in B"? Exist in *any* cell of B? Exist in the *corresponding* cell?
> >
> > What do you expect the result for?
> >
> > A = {[1 2 3] [4 5] [1 8] [11 12]}
> > B = {[4 1], [6 7 8] [9 10] [12 11]}
> >
> > Bruno
>
> I mean the value of A exist in the corresponding cell.
>
> A={[923000 [938000] ...... [.....until cell 100
> 936000 1098000 ....
> 944000 1120000 ....
> 1020000 1309000 ....]
> 1234000 1456098
> 1509800 1987897
> 1987000 2220990
> 2398000] 2340900
> 2678900
> 2798799]
> B has similar form but first cell has 71 rows, second 76 rows etc like i described above.
> So for example i want to take each value of the first cell of A and search if exists in first cell of B.For this i try the code i wrote in my first message.

A = {[1 2 3] [4 5] [1 8] [11 12]}
B = {[4 1], [6 7 8] [9 10] [12 11]}

cellfun(@(a,b) any(ismember(a,b)), A, B)

% OR

 ~cellfun(@(a,b) isempty(intersect(a,b)), A, B)

% Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us