knowledge-database (beta)

Current group: comp.soft-sys.matlab

Assigning values to a matrix at position specified by a vector

Assigning values to a matrix at position specified by a vector  
Pradeep Aggarwal
 Re: Assigning values to a matrix at position speci  
Mike Nospam
From:Pradeep Aggarwal
Subject:Assigning values to a matrix at position specified by a vector
Date:Sun, 23 Jan 2005 13:25:56 -0500
Hi,

I hope someone might be able to help me out with the
following problem I am facing:

I have a matrix A (e.g. A=zeros(4,4);)
and a vector b (e.g. b=[2 3];)

Now I want to put some value, say 80 in A, at the position given by
b. (i.e. I want A(2,3)=80;).

How do I do this. Please note the fact that the dimension
of A and b are not fixed, and can take any (mutually compatible)
value. That rules out using -- A(b(1), b(2))=80

Thanks in advance.

Pradeep
From:Mike Nospam
Subject:Re: Assigning values to a matrix at position speci
Date:Sun, 23 Jan 2005 18:57:13 -0500
One way to do it is the sparse command:

>> b=[2 3; 1 4; 5 1;6 2]

b =

2 3
1 4
5 1
6 2

>> s=rand(4,1)

s =

0.9501
0.2311
0.6068
0.4860

>> A=full(sparse(b(:,1),b(:,2),s))

A =

0 0 0 0.2311
0 0 0.9501 0
0 0 0 0
0 0 0 0
0.6068 0 0 0
0 0.4860 0 0

Pradeep Aggarwal wrote:
>
>
> Hi,
>
> I hope someone might be able to help me out with the
> following problem I am facing:
>
> I have a matrix A (e.g. A=zeros(4,4);)
> and a vector b (e.g. b=[2 3];)
>
> Now I want to put some value, say 80 in A, at the position given by
> b. (i.e. I want A(2,3)=80;).
>
> How do I do this. Please note the fact that the dimension
> of A and b are not fixed, and can take any (mutually compatible)
> value. That rules out using -- A(b(1), b(2))=80
>
> Thanks in advance.
>
> Pradeep
   

Copyright © 2006 knowledge-database   -   All rights reserved