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:
Changing multiple matrix element at one time

Subject: Changing multiple matrix element at one time

From: Patrick A.

Date: 15 Oct, 2010 15:32:03

Message: 1 of 5

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

Subject: Changing multiple matrix element at one time

From: Oleg Komarov

Date: 15 Oct, 2010 15:39:04

Message: 2 of 5

"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

Subject: Changing multiple matrix element at one time

From: Patrick A.

Date: 15 Oct, 2010 16:08:04

Message: 3 of 5

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

Subject: Changing multiple matrix element at one time

From: Sean

Date: 15 Oct, 2010 16:13:03

Message: 4 of 5


> 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

Subject: Changing multiple matrix element at one time

From: Loren Shure

Date: 18 Oct, 2010 12:55:11

Message: 5 of 5


"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

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