Thread Subject: Good way to compare two cell arrays???

Subject: Good way to compare two cell arrays???

From: Darin

Date: 2 Sep, 2010 21:49:04

Message: 1 of 4

Hello,

Your advice is needed to help figure out an eloquent solution to compare two cell arrays (that contain strings). The first cell array (files) needs to be shrunk down whenever it exists in the second array (finishedfiles). The final shrunken down array is called newfiles. Is there a better way to do this???

count2= 0;
        for i = 1:numel(files)
            
            count = 0;
            for j = 1:numel(finishedfiles)
                if strcmp(files{i},finishedfiles{j})
                    count=1;
                    break;
                end
            end
            
            if count == 0;
                count2 = count2+1;
                newfiles{count2} = files{i};
            end
            
        end

Subject: Good way to compare two cell arrays???

From: TideMan

Date: 2 Sep, 2010 22:54:01

Message: 2 of 4

On Sep 3, 9:49 am, "Darin " <dmc...@mail.bradley.edu> wrote:
> Hello,
>
> Your advice is needed to help figure out an eloquent solution to compare two cell arrays (that contain strings). The first cell array (files) needs to be shrunk down whenever it exists in the second array (finishedfiles). The final shrunken down array is called newfiles. Is there a better way to do this???
>
> count2= 0;
>         for i = 1:numel(files)
>
>             count = 0;
>             for j = 1:numel(finishedfiles)
>                 if strcmp(files{i},finishedfiles{j})
>                     count=1;
>                     break;
>                 end
>             end
>
>             if count == 0;
>                 count2 = count2+1;
>                 newfiles{count2} = files{i};
>             end
>
>         end

help intersect

Subject: Good way to compare two cell arrays???

From: Roland

Date: 2 Sep, 2010 23:30:21

Message: 3 of 4

"Darin " <dmccoy@mail.bradley.edu> wrote in message <i5p64g$nsl$1@fred.mathworks.com>...
> Hello,
>
> Your advice is needed to help figure out an eloquent solution to compare two cell arrays (that contain strings). The first cell array (files) needs to be shrunk down whenever it exists in the second array (finishedfiles). The final shrunken down array is called newfiles. Is there a better way to do this???
>
> count2= 0;
> for i = 1:numel(files)
>
> count = 0;
> for j = 1:numel(finishedfiles)
> if strcmp(files{i},finishedfiles{j})
> count=1;
> break;
> end
> end
>
> if count == 0;
> count2 = count2+1;
> newfiles{count2} = files{i};
> end
>
> end

not 100% sure, but i think this should do what you want

clc
clear all

files={'file1';'file2';'file3'};
finishedfiles={'file3';'file2'};

a=repmat(files,size(finishedfiles));
b=sort(repmat(finishedfiles,size(files)));

c=(strcmp(a,b));

d=reshape(c,size(files,1),size(c,1)/size(files,1));

newfiles=files(~logical(sum(d,2)))

Roland

Subject: Good way to compare two cell arrays???

From: Darin

Date: 3 Sep, 2010 13:41:09

Message: 4 of 4

Thanks! Turns out i could compress those 14 lines into 1 line.....

files = setdiff(files,finishedfiles);

good times :)

TideMan <mulgor@gmail.com> wrote in message <0da82bc4-f051-44cf-8789-840a4bb3ba6a@x18g2000pro.googlegroups.com>...
> On Sep 3, 9:49 am, "Darin " <dmc...@mail.bradley.edu> wrote:
> > Hello,
> >
> > Your advice is needed to help figure out an eloquent solution to compare two cell arrays (that contain strings). The first cell array (files) needs to be shrunk down whenever it exists in the second array (finishedfiles). The final shrunken down array is called newfiles. Is there a better way to do this???
> >
> > count2= 0;
> >         for i = 1:numel(files)
> >
> >             count = 0;
> >             for j = 1:numel(finishedfiles)
> >                 if strcmp(files{i},finishedfiles{j})
> >                     count=1;
> >                     break;
> >                 end
> >             end
> >
> >             if count == 0;
> >                 count2 = count2+1;
> >                 newfiles{count2} = files{i};
> >             end
> >
> >         end
>
> help intersect

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com