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:
vector and array

Subject: vector and array

From: Ramzi

Date: 15 Feb, 2011 03:30:38

Message: 1 of 8

Hello everybody
How can I do the following without loop?

N=5;
M=4;

x=rand(N,M)>0.5;
y=rand(1,M)>0.5;
% I hate this loop
for i=1:N
   z(i,:)=y & x(i,:);
end

i.e I want to AND vector y with each row in x

Subject: vector and array

From: Matt Fig

Date: 15 Feb, 2011 04:27:03

Message: 2 of 8

z = bsxfun(@and,x,y);

Subject: vector and array

From: Yumnam Kirani

Date: 15 Feb, 2011 05:55:05

Message: 3 of 8

Ramzi <ramzib2@gmail.com> wrote in message <c8b70a4e-96e2-4aca-8f5f-396c40ddb714@z20g2000yqe.googlegroups.com>...
> Hello everybody
> How can I do the following without loop?
>
> N=5;
> M=4;
>
> x=rand(N,M)>0.5;
> y=rand(1,M)>0.5;
> % I hate this loop
> for i=1:N
> z(i,:)=y & x(i,:);
> end
>
> i.e I want to AND vector y with each row in x
You can make y have the same size as x by making each row has y. Then simply do anding on x and y.

Yumnam Kirani Singh
Tronglaobi Awang Leikai

Subject: vector and array

From: Ramzi

Date: 15 Feb, 2011 13:50:42

Message: 4 of 8

On Feb 15, 6:55 am, "Yumnam Kirani" <kirani.si...@gmail.com> wrote:
> Ramzi <ramz...@gmail.com> wrote in message <c8b70a4e-96e2-4aca-8f5f-396c40ddb...@z20g2000yqe.googlegroups.com>...
> > Hello everybody
> > How can I do the following without loop?
>
> > N=5;
> > M=4;
>
> > x=rand(N,M)>0.5;
> > y=rand(1,M)>0.5;
> > % I hate this loop
> > for i=1:N
> >    z(i,:)=y & x(i,:);
> > end
>
> > i.e I want to AND vector y with each row in x
>
> You can make y have the same size as x by making each row has y. Then simply do anding on x and y.
>
> Yumnam Kirani Singh
> Tronglaobi Awang Leikai

Do you mean using repmat? I did that, but I want another way.
BR

Subject: vector and array

From: Ramzi

Date: 15 Feb, 2011 14:22:14

Message: 5 of 8

On Feb 15, 5:27 am, "Matt Fig" <spama...@yahoo.com> wrote:
> z  = bsxfun(@and,x,y);

Thanks Matt. It works.
Yesterday I tried bsxfun(@and,y,x) and it gave wrong answer. So I
didn't try to exchange x and y.
Why bsxfun(@and,y,x) do not equal to bsxfun(@and,x,y)?

Subject: vector and array

From: Jan Simon

Date: 15 Feb, 2011 14:37:04

Message: 6 of 8

Dear Ramzi,

> Do you mean using repmat? I did that, but I want another way.

It yould be kind to show us exactly, what you did and explain, why you want something else. Instead of calling REPMAT, you can expand the vector by using ONES directly. But I assume BSXFUN as suggested by Matt will b the most efficient solution.

Kind regards, Jan

Subject: vector and array

From: Ramzi

Date: 16 Feb, 2011 03:55:08

Message: 7 of 8

On Feb 15, 3:37 pm, "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de>
wrote:
> Dear Ramzi,
>
> > Do you mean using repmat? I did that, but I want another way.
>
> It yould be kind to show us exactly, what you did and explain, why you want something else. Instead of calling REPMAT, you can expand the vector by using ONES directly. But I assume BSXFUN as suggested by Matt will b the most efficient solution.
>
> Kind regards, Jan

But why bsxfun(@and,y,x) do not equal to bsxfun(@and,x,y)?

Subject: vector and array

From: Matt Fig

Date: 16 Feb, 2011 04:37:03

Message: 8 of 8

Ramzi <ramzib2@gmail.com> wrote in message <5ecc5ef0-0aca-
> But why bsxfun(@and,y,x) do not equal to bsxfun(@and,x,y)?

I don't know what you mean. Did you correctly test this?


N=5;
M=4;

for ii = 1:1000
    x=rand(N,M)>0.5;
    y=rand(1,M)>0.5;
    if ~isequal(bsxfun(@and,y,x),bsxfun(@and,x,y))
        disp('Not Equal!') % Never happens....
        break
    end
end

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