Changing multiple matrix element at one time

  • Follow


Hey,

I'm trying to modify multiple matrix's elements at a time. considering the matrix a = zeros(3), I want to set the two values a(1, 2) and a(3, 3) to 1.

Instead of doing a(1,2) = 1, and then a(3,3) = 1, is there a way to do that on one line ?

I tried different variants of a([1 3], [2 3]) but it never gives me what I expect. (it modifies all combinations of both vectors, (1,2) (1,3) (3,2) (3,3)). I must be close, any hint anyone ? Thanks.

Greetings,
Patrick
0
Reply Patrick 10/15/2010 3:32:03 PM

"Patrick A." <m.maverick@gmail.com> wrote in message <i99s5j$ipo$1@fred.mathworks.com>...
> Hey,
> 
> I'm trying to modify multiple matrix's elements at a time. considering the matrix a = zeros(3), I want to set the two values a(1, 2) and a(3, 3) to 1.
> 
> Instead of doing a(1,2) = 1, and then a(3,3) = 1, is there a way to do that on one line ?
> 
> I tried different variants of a([1 3], [2 3]) but it never gives me what I expect. (it modifies all combinations of both vectors, (1,2) (1,3) (3,2) (3,3)). I must be close, any hint anyone ? Thanks.
> 
> Greetings,
> Patrick

A hint:

sub2ind

Oleg
0
Reply Oleg 10/15/2010 3:39:04 PM


That's great thanks.

Another question, I have a 3D matrix, with only 2 dimension with arrays (first dimension fixed)

So for now I use something like :

a=rand(2, 3, 2);
d1=2;
d2=[1 2];
d3=[1 2];
a(sub2ind(size(a), linspace(d1, d1, length(d2)), d2, d3)) = 1;

this works but I don't feel comfortable with this linspace thing, is there a better way to generate a vector of x of length y ?

Greetings,
Patrick
0
Reply Patrick 10/15/2010 4:08:04 PM

> a=rand(2, 3, 2);
> d1=2;
> d2=[1 2];
> d3=[1 2];
> a(sub2ind(size(a), linspace(d1, d1, length(d2)), d2, d3)) = 1;
> this works but I don't feel comfortable with this linspace thing, is there a better way to generate a vector of x of length y ?

help repmat
help meshgrid
0
Reply Sean 10/15/2010 4:13:03 PM

"Patrick A." <m.maverick@gmail.com> wrote in message 
news:i99u94$8un$1@fred.mathworks.com...
> That's great thanks.
>
> Another question, I have a 3D matrix, with only 2 dimension with arrays 
> (first dimension fixed)
>
> So for now I use something like :
>
> a=rand(2, 3, 2);
> d1=2;
> d2=[1 2];
> d3=[1 2];
> a(sub2ind(size(a), linspace(d1, d1, length(d2)), d2, d3)) = 1;
>
> this works but I don't feel comfortable with this linspace thing, is there 
> a better way to generate a vector of x of length y ?
>
> Greetings,
> Patrick
>

use indexing

num = 3
twelvenums = num(ones(1,12))

-- 
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ 

0
Reply loren.shure (822) 10/18/2010 12:55:11 PM

4 Replies
368 Views

(page loaded in 0.037 seconds)

Similiar Articles:













7/26/2012 2:10:11 PM


Reply: