Thread Subject: How to reorganize a vector into a matrix without a for loop?

Subject: How to reorganize a vector into a matrix without a for loop?

From: Kian

Date: 2 May, 2009 22:42:01

Message: 1 of 7

Hi,

I have a vector:

a = [0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0];

I'd like to organize this vector into a matrix with the following criteria: every time I see a 1 I want to copy that 1 and 2 other members before it into a new line, so my resulting matrix will look like this:

b = [0 0 1
       0 1 1
       0 0 1
       0 0 1
       0 0 1
       1 0 1
       0 0 1
       0 0 1
       1 0 1
       0 0 1
       0 0 1];

Right now I do it using a for loop:

spikeIndeces = find(a == 1);
for i=1:length(spikeIndeces)
    b(i,:) = a(spikeIndeces(i)-2:spikeIndeces(i));
end

I'd like to do this without a loop, in as little lines as possible and as fast as possible.

Thanks in advance!

Kian

Subject: How to reorganize a vector into a matrix without a for loop?

From: Bruno Luong

Date: 2 May, 2009 23:03:04

Message: 2 of 7

a = [0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0];

% pad a margin for safety
apad=[0 0 a]

i1 = find(apad==1);
idx=bsxfun(@plus,[-2:0]',i1)
b=reshape(apad(idx(:)),3,[])'

% Bruno

Subject: How to reorganize a vector into a matrix without a for loop?

From: Bruno Luong

Date: 2 May, 2009 23:17:01

Message: 3 of 7

% Slightly better:

apad=[0 0 a]

i1 = find(apad==1);
b=apad(bsxfun(@plus,i1(:),(-2:0)))

% Bruno

Subject: How to reorganize a vector into a matrix without a for loop?

From: Bruno Luong

Date: 2 May, 2009 23:33:01

Message: 4 of 7

If you use my FEX submission bsxops and have this option enable
http://www.mathworks.com/matlabcentral/fileexchange/23821
Then the code is even more compact and readable:

i1 = find(apad==1);
b=apad(i1(:)+(-2:0))

% Bruno

Subject: How to reorganize a vector into a matrix without a for loop?

From: Kian

Date: 4 Jun, 2009 22:01:02

Message: 5 of 7

When I tested this a while ago (didn't actually use it in what I wanted) I remember that it worked fine and I was really excited. Today, I wanted to actually use it, but I keep getting the "??? Undefined function or variable 'bsxfun'." error message. Do you know why? Do I need to install a specific function in MATLAB?

Also, I don't know how to use your FEX submission. Do I just download and add it to my path?

Is it possible to achieve this without using any extra functions to make sure that it works on every machine without having to install additional functions?

Thanks!
Kian

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gtil7d$bv4$1@fred.mathworks.com>...
> If you use my FEX submission bsxops and have this option enable
> http://www.mathworks.com/matlabcentral/fileexchange/23821
> Then the code is even more compact and readable:
>
> i1 = find(apad==1);
> b=apad(i1(:)+(-2:0))
>
> % Bruno

Subject: How to reorganize a vector into a matrix without a for loop?

From: Kian

Date: 4 Jun, 2009 22:09:02

Message: 6 of 7

Apparently, I don't have bsxfun as a function. What toolbox does it come in? I have a complete version of MATLAB with all toolboxes installed. Any idea why?

I have R2006b installed.

Subject: How to reorganize a vector into a matrix without a for loop?

From: Bruno Luong

Date: 4 Jun, 2009 22:23:02

Message: 7 of 7

"Kian " <kian.torab@utah.edu> wrote in message <h09glu$45$1@fred.mathworks.com>...
> Apparently, I don't have bsxfun as a function. What toolbox does it come in? I have a complete version of MATLAB with all toolboxes installed. Any idea why?
>
> I have R2006b installed.

Kian,

bsxfun is Matlab builtin function existing from 2007A.
For previous Matlab version, you might use REPMAT to accomplish the same task:

apad=[0 0 a]
i1 = find(apad==1);
b=apad(repmat(i1(:),1,3)+repmat(-2:0,numel(i1),1))

% Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
matrix matrices... Kian 2 May, 2009 18:44:04
rssFeed for this Thread

Contact us at files@mathworks.com