Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

4 views
Skip to first unread message

Kian

unread,
May 2, 2009, 6:42:01 PM5/2/09
to
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

Bruno Luong

unread,
May 2, 2009, 7:03:04 PM5/2/09
to
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

Bruno Luong

unread,
May 2, 2009, 7:17:01 PM5/2/09
to
% Slightly better:

apad=[0 0 a]

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

% Bruno

Bruno Luong

unread,
May 2, 2009, 7:33:01 PM5/2/09
to
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

Kian

unread,
Jun 4, 2009, 6:01:02 PM6/4/09
to
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.l...@fogale.findmycountry> wrote in message <gtil7d$bv4$1...@fred.mathworks.com>...

Kian

unread,
Jun 4, 2009, 6:09:02 PM6/4/09
to
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.

Bruno Luong

unread,
Jun 4, 2009, 6:23:02 PM6/4/09
to
"Kian " <kian....@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

0 new messages