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:
Array problem

Subject: Array problem

From: Liana

Date: 31 Oct, 2011 09:16:11

Message: 1 of 3

Hi,

I have the following problem. Let's say there is an 3x3 array 'arr':
0 0 0
0 0 0
0 0 0

There are 3 edges 'E':
1 2
1 3
2 3

Finally, there are 3 logical values 'val' that correspond to edges 'E':
1
0
1

Now I need to process 'arr' in such a way that all edges that have '1' are defined in 'arr', e.g. if E(1,:) = 1 2 and val(1) = 1, then the cell [1,2] in 'arr' has a value '1'. The output should be:
arr=
0 1 1
0 0 1
0 0 0

I wrote the following code, but the last line does not work properly. Any help is highly appreciated. Thanks.
array = [0 0 0; 0 0 0; 0 0 0];
E = [1 2; 1 3; 2 3];
val = [1; 0; 1];
len = length(E(:,1));
array(E(1:len,1),E(1:len,2)) = val(1:len,1);

Subject: Array problem

From: Matt J

Date: 31 Oct, 2011 13:21:11

Message: 2 of 3

"Liana" wrote in message <j8lp0r$efa$1@newscl01ah.mathworks.com>...
>
> There are 3 edges 'E':
> 1 2
> 1 3
> 2 3
>
> Finally, there are 3 logical values 'val' that correspond to edges 'E':
> 1
> 0
> 1
>
> Now I need to process 'arr' in such a way that all edges that have '1' are defined in 'arr', e.g. if E(1,:) = 1 2 and val(1) = 1, then the cell [1,2] in 'arr' has a value '1'. The output should be:
> arr=
> 0 1 1
> 0 0 1
> 0 0 0
==============

I think you have an error in this example. Only 2 of the 3 vals are one, yet you have three 1s in the array.

In any case, it sounds like you would just use SUB2IND to convert E into a linear index. Then use it to assign val into arr.

Subject: Array problem

From: Liana

Date: 31 Oct, 2011 17:40:27

Message: 3 of 3

Thanks.
"Matt J" wrote in message <j8m7c7$r6t$1@newscl01ah.mathworks.com>...
> "Liana" wrote in message <j8lp0r$efa$1@newscl01ah.mathworks.com>...
> >
> > There are 3 edges 'E':
> > 1 2
> > 1 3
> > 2 3
> >
> > Finally, there are 3 logical values 'val' that correspond to edges 'E':
> > 1
> > 0
> > 1
> >
> > Now I need to process 'arr' in such a way that all edges that have '1' are defined in 'arr', e.g. if E(1,:) = 1 2 and val(1) = 1, then the cell [1,2] in 'arr' has a value '1'. The output should be:
> > arr=
> > 0 1 1
> > 0 0 1
> > 0 0 0
> ==============
>
> I think you have an error in this example. Only 2 of the 3 vals are one, yet you have three 1s in the array.
>
> In any case, it sounds like you would just use SUB2IND to convert E into a linear index. Then use it to assign val into arr.

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