Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Insert values into a matrix
Replies: 2   Last Post: Jul 15, 2013 8:04 PM

 Messages: [ Previous | Next ]
 Loren Shure Posts: 1,067 Registered: 12/7/04
Re: Insert values into a matrix
Posted: Jul 15, 2013 7:40 PM

"Max " <mshestov@gmail.com> wrote in message
news:ks201c\$bok\$1@newscl01ah.mathworks.com...
> Hi guys, I'm having a bit of a brain fart here.
>
> Say you have a matrix A = [1,2,3,4,5]
> and a logical vector B = [1,0,0,1,1,1,0,1]
>
> Where I want to stretch A to the Length of B by inserting 0's where 0's
> locations appear in B.
>
> Basically, how can I turn matrix A into this [1,0,0,2,3,4,0,5] using B
>
> Appreciate the help guys.
>
> -Max
>

Try this:

A = [1,2,3,4,5]
B = [1,0,0,1,1,1,0,1]
B = logical(B);
C(B) = A
C =
1 0 0 2 3 4 0 5

--
--Loren

http://blogs.mathworks.com/loren

Date Subject Author
7/15/13 Max
7/15/13 Loren Shure
7/15/13 Max