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:
Matrix Manipulation

Subject: Matrix Manipulation

From: Berty

Date: 21 Aug, 2010 13:35:04

Message: 1 of 5

I have a matrix A which is 6x3 zeroes
I have matrix B which is 1x3
consisting of the following numbers B= (2,4,2)
What I want is to do is create values 1 using the values in matrix B as starting points for 3 consecutive numbers in each column?

So what I want to end up with is

0 0 0
1 0 1
1 0 1
1 1 1
0 1 0
0 1 0

What i have tried doin is A(B:B+3)=1
This only does it for the first column

A(B:B+3,:)=1
this just gives 1 values for each column begining at row 2

0 0 0
1 1 1
1 1 1
1 1 1
0 0 0
0 0 0
Any ideas about how to do this would be very helpful?

Subject: Matrix Manipulation

From: Matt J

Date: 21 Aug, 2010 14:26:04

Message: 2 of 5


for ii=1:3
 A(B(ii)+(0:2),:)=1;
end

Subject: Matrix Manipulation

From: Matt J

Date: 21 Aug, 2010 14:51:04

Message: 3 of 5

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4onls$j75$1@fred.mathworks.com>...
>
> for ii=1:3
> A(B(ii)+(0:2),:)=1;
> end

Sorry, that should be

for ii=1:3
  A(B(ii)+(0:2),ii)=1;
end

Subject: Matrix Manipulation

From: Barry

Date: 21 Aug, 2010 15:06:04

Message: 4 of 5

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4onls$j75$1@fred.mathworks.com>...
>
> for ii=1:3
> A(B(ii)+(0:2),:)=1;
> end

That doesnt work that returns
0 0 0
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

Why did you call ii=1:3 thats the same as ii=3

what i want is
0 0 0
1 0 1
1 0 1
1 1 1
0 1 0
0 1 0

ie the first column's row 2 (ie first number in B) changes from 0 to 1 and the three rows after in that column
and the next 2 columns change similarly with column 2 begining at row 3 according to matrix B
and column 3 begining at row 2 according to matrix B

ALSO sorry B=[2,3,2]

Subject: Matrix Manipulation

From: Barry

Date: 21 Aug, 2010 15:15:07

Message: 5 of 5

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4op4o$lui$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i4onls$j75$1@fred.mathworks.com>...
> >
> > for ii=1:3
> > A(B(ii)+(0:2),:)=1;
> > end
>
> Sorry, that should be
>
> for ii=1:3
> A(B(ii)+(0:2),ii)=1;
> end

Ya that second one works. Thanks very much i greatly appreciate it

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