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:
Updating matrix

Subject: Updating matrix

From: Ulrik Nash

Date: 10 Sep, 2010 07:29:03

Message: 1 of 10

Hi Everyone,

Suppose I have:

A = [1 2 0 4 0 0]

and

B = [1 2 3 6]

Now I need to update A with information in B to create:

A = [1 2 3 0 0 6]

You will notice the following:

* The numbers in B indicate the index-values in A
* If an none-zero value is present in both A and B, then that value essentially remains.
* If there is a none-zero value in A that is not present in B, then that value is set to zero.

Do you guys have a neat and efficient procedure, which performs task?

Kind regards,

Ulrik.

Subject: Updating matrix

From: Walter Roberson

Date: 10 Sep, 2010 07:40:37

Message: 2 of 10

On 10/09/10 2:29 AM, Ulrik Nash wrote:

> Suppose I have:
>
> A = [1 2 0 4 0 0]
>
> and
>
> B = [1 2 3 6]
>
> Now I need to update A with information in B to create:
>
> A = [1 2 3 0 0 6]
>
> You will notice the following:
>
> * The numbers in B indicate the index-values in A
> * If an none-zero value is present in both A and B, then that value
> essentially remains.
> * If there is a none-zero value in A that is not present in B, then that
> value is set to zero.
>
> Do you guys have a neat and efficient procedure, which performs task?

setintersect() can tell you which values are in both matrices.

ismember() can tell you which values of one matrix are present in another.

Consider for example

A(~ismember(A,B)) = 0;

Subject: Updating matrix

From: Ulrik Nash

Date: 10 Sep, 2010 08:12:29

Message: 3 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <Whlio.23462$u16.16594@newsfe17.iad>...
> On 10/09/10 2:29 AM, Ulrik Nash wrote:
>
> > Suppose I have:
> >
> > A = [1 2 0 4 0 0]
> >
> > and
> >
> > B = [1 2 3 6]
> >
> > Now I need to update A with information in B to create:
> >
> > A = [1 2 3 0 0 6]
> >
> > You will notice the following:
> >
> > * The numbers in B indicate the index-values in A
> > * If an none-zero value is present in both A and B, then that value
> > essentially remains.
> > * If there is a none-zero value in A that is not present in B, then that
> > value is set to zero.
> >
> > Do you guys have a neat and efficient procedure, which performs task?
>
> setintersect() can tell you which values are in both matrices.
>
> ismember() can tell you which values of one matrix are present in another.
>
> Consider for example
>
> A(~ismember(A,B)) = 0;



Hi Walter, thanks again for your help. I think this is very useful.

As I see it, however, this procedure changes A in the process.

I was thinking the following to find what is in B but not in A:

B(ismember(A,B)) = 0;

which gives B = 0 0 3 6

but now I still need to find what is in A and not in (the original) B. But the original B is gone...

How do I overcome this dilemma?

Subject: Updating matrix

From: Ulrik Nash

Date: 10 Sep, 2010 08:36:12

Message: 4 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cp9d$8pk$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <Whlio.23462$u16.16594@newsfe17.iad>...
> > On 10/09/10 2:29 AM, Ulrik Nash wrote:
> >
> > > Suppose I have:
> > >
> > > A = [1 2 0 4 0 0]
> > >
> > > and
> > >
> > > B = [1 2 3 6]
> > >
> > > Now I need to update A with information in B to create:
> > >
> > > A = [1 2 3 0 0 6]
> > >
> > > You will notice the following:
> > >
> > > * The numbers in B indicate the index-values in A
> > > * If an none-zero value is present in both A and B, then that value
> > > essentially remains.
> > > * If there is a none-zero value in A that is not present in B, then that
> > > value is set to zero.
> > >
> > > Do you guys have a neat and efficient procedure, which performs task?
> >
> > setintersect() can tell you which values are in both matrices.
> >
> > ismember() can tell you which values of one matrix are present in another.
> >
> > Consider for example
> >
> > A(~ismember(A,B)) = 0;
>
>
>
> Hi Walter, thanks again for your help. I think this is very useful.
>
> As I see it, however, this procedure changes A in the process.
>
> I was thinking the following to find what is in B but not in A:
>
> B(ismember(A,B)) = 0;
>
> which gives B = 0 0 3 6
>
> but now I still need to find what is in A and not in (the original) B. But the original B is gone...
>
> How do I overcome this dilemma?

I mean, I could do some intermediate procedure, like duplicating matrixes, but is that the most efficient way?

Again, thanks for all the help.

Subject: Updating matrix

From: Ulrik Nash

Date: 10 Sep, 2010 08:44:08

Message: 5 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cqls$7vc$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cp9d$8pk$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <Whlio.23462$u16.16594@newsfe17.iad>...
> > > On 10/09/10 2:29 AM, Ulrik Nash wrote:
> > >
> > > > Suppose I have:
> > > >
> > > > A = [1 2 0 4 0 0]
> > > >
> > > > and
> > > >
> > > > B = [1 2 3 6]
> > > >
> > > > Now I need to update A with information in B to create:
> > > >
> > > > A = [1 2 3 0 0 6]
> > > >
> > > > You will notice the following:
> > > >
> > > > * The numbers in B indicate the index-values in A
> > > > * If an none-zero value is present in both A and B, then that value
> > > > essentially remains.
> > > > * If there is a none-zero value in A that is not present in B, then that
> > > > value is set to zero.
> > > >
> > > > Do you guys have a neat and efficient procedure, which performs task?
> > >
> > > setintersect() can tell you which values are in both matrices.
> > >
> > > ismember() can tell you which values of one matrix are present in another.
> > >
> > > Consider for example
> > >
> > > A(~ismember(A,B)) = 0;
> >
> >
> >
> > Hi Walter, thanks again for your help. I think this is very useful.
> >
> > As I see it, however, this procedure changes A in the process.
> >
> > I was thinking the following to find what is in B but not in A:
> >
> > B(ismember(A,B)) = 0;
> >
> > which gives B = 0 0 3 6
> >
> > but now I still need to find what is in A and not in (the original) B. But the original B is gone...
> >
> > How do I overcome this dilemma?
>
> I mean, I could do some intermediate procedure, like duplicating matrixes, but is that the most efficient way?
>
> Again, thanks for all the help.

Okay, I solved this in my (probably ad hoc) fashion!

A = [1 2 0 4 0 0]
B = [1 2 3 6]
C = A
C(ismember(C,B)) = 0
C(C == 0) = []
A(1,C) = 0
A(1,B) = B

Now my 'challenge' for you guys is to improve my code :-)

Kind regards,

Ulrik

Subject: Updating matrix

From: Bruno Luong

Date: 10 Sep, 2010 09:26:06

Message: 6 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cr4o$7lr$1@fred.mathworks.com>...
>
> A = [1 2 0 4 0 0]
> B = [1 2 3 6]
> C = A
> C(ismember(C,B)) = 0
> C(C == 0) = []
> A(1,C) = 0
> A(1,B) = B
>

B = [1 2 3 6]
clear A
A(B) = B

Bruno

Subject: Updating matrix

From: Oleg Komarov

Date: 10 Sep, 2010 09:30:09

Message: 7 of 10

> Okay, I solved this in my (probably ad hoc) fashion!
>
> A = [1 2 0 4 0 0]
> B = [1 2 3 6]
> C = A
> C(ismember(C,B)) = 0
> C(C == 0) = []
> A(1,C) = 0
> A(1,B) = B
>
> Now my 'challenge' for you guys is to improve my code :-)
>
> Kind regards,
>
> Ulrik

Well, the challenge's up to you.

Oleg

Subject: Updating matrix

From: Ulrik Nash

Date: 10 Sep, 2010 09:33:13

Message: 8 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <i6ctjd$f34$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cr4o$7lr$1@fred.mathworks.com>...
> >
> > A = [1 2 0 4 0 0]
> > B = [1 2 3 6]
> > C = A
> > C(ismember(C,B)) = 0
> > C(C == 0) = []
> > A(1,C) = 0
> > A(1,B) = B
> >
>
> B = [1 2 3 6]
> clear A
> A(B) = B
>
> Bruno


Haha, not much of a challenge it seems! Thanks Bruno!

Kind regard Ulrik.

Subject: Updating matrix

From: Ulrik Nash

Date: 10 Sep, 2010 09:49:05

Message: 9 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cu0p$bmk$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <i6ctjd$f34$1@fred.mathworks.com>...
> > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cr4o$7lr$1@fred.mathworks.com>...
> > >
> > > A = [1 2 0 4 0 0]
> > > B = [1 2 3 6]
> > > C = A
> > > C(ismember(C,B)) = 0
> > > C(C == 0) = []
> > > A(1,C) = 0
> > > A(1,B) = B
> > >
> >
> > B = [1 2 3 6]
> > clear A
> > A(B) = B
> >
> > Bruno
>
>
> Haha, not much of a challenge it seems! Thanks Bruno!
>
> Kind regard Ulrik.

But wait! The length of A should remain unchanged throughout. Your solution Bruno has a lenth equal to the length of the last digit in B, which is not necessarily the lenth of A.

It appears a matter of adding zeros to fill out, but how would you suggest I do this?

Subject: Updating matrix

From: Bruno Luong

Date: 10 Sep, 2010 10:03:06

Message: 10 of 10

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cuug$alp$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cu0p$bmk$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <i6ctjd$f34$1@fred.mathworks.com>...
> > > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i6cr4o$7lr$1@fred.mathworks.com>...
> > > >
> > > > A = [1 2 0 4 0 0]
> > > > B = [1 2 3 6]
> > > > C = A
> > > > C(ismember(C,B)) = 0
> > > > C(C == 0) = []
> > > > A(1,C) = 0
> > > > A(1,B) = B
> > > >
> > >
> > > B = [1 2 3 6]
> > > clear A
> > > A(B) = B
> > >
> > > Bruno
> >
> >
> > Haha, not much of a challenge it seems! Thanks Bruno!
> >
> > Kind regard Ulrik.
>
> But wait! The length of A should remain unchanged throughout. Your solution Bruno has a lenth equal to the length of the last digit in B, which is not necessarily the lenth of A.
>
> It appears a matter of adding zeros to fill out, but how would you suggest I do this?

Replace "clear A" by "A(:) = 0".

Bruno

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