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 unequal column matrix in matlab without using for loop

Subject: Compare two unequal column matrix in matlab without using for loop

From: MD

Date: 30 Oct, 2012 04:30:08

Message: 1 of 4

I want to compare two columns matrix : such as a=[1;2;2;3;4;4;5;6] and b=[2;4;8] and get the output like d=[0;1;1;0;1;1;0;0] which is same number of rows of matrix a.

Subject: Compare two unequal column matrix in matlab without using for

From: Nasser M. Abbasi

Date: 30 Oct, 2012 05:10:49

Message: 2 of 4

On 10/29/2012 11:30 PM, MD wrote:
> I want to compare two columns matrix : such as a=[1;2;2;3;4;4;5;6] and b=[2;4;8] and
>get the output like d=[0;1;1;0;1;1;0;0] which is same number of rows of matrix a.
>

may be a loop?

------------------------------
a=[1;2;2;3;4;4;5;6];
b=[2;4;8];
c=zeros(length(a),1);
for i=1:length(a)
     if isempty(find(b==a(i),1))
         c(i)=0;
     else
         c(i)=1;
     end
end
-----------------------------

c =

      0
      1
      1
      0
      1
      1
      0
      0

--Nasser

Subject: Compare two unequal column matrix in matlab without using for

From: Nasser M. Abbasi

Date: 30 Oct, 2012 05:13:54

Message: 3 of 4

On 10/30/2012 12:10 AM, Nasser M. Abbasi wrote:
> On 10/29/2012 11:30 PM, MD wrote:
>> I want to compare two columns matrix : such as a=[1;2;2;3;4;4;5;6] and b=[2;4;8] and
>> get the output like d=[0;1;1;0;1;1;0;0] which is same number of rows of matrix a.
>>
>

Oh, I just same that you do NOT want to use a loop.

Ok. back to the drawing board ;) But what is wrong with a loop?
You can use arrayfun(), but not sure it will be faster.


> may be a loop?
>
> ------------------------------
> a=[1;2;2;3;4;4;5;6];
> b=[2;4;8];
> c=zeros(length(a),1);
> for i=1:length(a)
> if isempty(find(b==a(i),1))
> c(i)=0;
> else
> c(i)=1;
> end
> end
> -----------------------------
>
> c =
>
> 0
> 1
> 1
> 0
> 1
> 1
> 0
> 0
>
> --Nasser
>

Subject: Compare two unequal column matrix in matlab without using for

From: Nasser M. Abbasi

Date: 30 Oct, 2012 05:26:11

Message: 4 of 4

On 10/30/2012 12:13 AM, Nasser M. Abbasi wrote:
> On 10/30/2012 12:10 AM, Nasser M. Abbasi wrote:
>> On 10/29/2012 11:30 PM, MD wrote:
>>> I want to compare two columns matrix : such as a=[1;2;2;3;4;4;5;6] and b=[2;4;8] and
>>> get the output like d=[0;1;1;0;1;1;0;0] which is same number of rows of matrix a.
>>>
>>
>
> Oh, I just same that you do NOT want to use a loop.
>
> Ok. back to the drawing board ;) But what is wrong with a loop?
> You can use arrayfun(), but not sure it will be faster.
>
>
>> may be a loop?
>>
>> ------------------------------
>> a=[1;2;2;3;4;4;5;6];
>> b=[2;4;8];
>> c=zeros(length(a),1);
>> for i=1:length(a)
>> if isempty(find(b==a(i),1))
>> c(i)=0;
>> else
>> c(i)=1;
>> end
>> end
>> -----------------------------
>>
>> c =
>>
>> 0
>> 1
>> 1
>> 0
>> 1
>> 1
>> 0
>> 0
>>
>> --Nasser
>>
>


Ok, here is without a loop

EDU>> ismember(a,b)

ans =

      0
      1
      1
      0
      1
      1
      0
      0

--Nasser

Tags for 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