Question

I have a vector x = [1,3,5,6,7] and I want to produce a matrix y in which rows y(k) = x(k:k+2). So the resulting matrix in this case would be

1 3 5
3 5 6
5 6 7

How can I achieve this without using a loop? Is there a clever way of doing it with indexing?

Was it helpful?

Solution

This is the top non-zero square of a Hankel matrix. Just use hankel:

>> X = hankel(x)
X =
     1     3     5     6     7
     3     5     6     7     0
     5     6     7     0     0
     6     7     0     0     0
     7     0     0     0     0
>> X = X(1:3,1:3)
X =
     1     3     5
     3     5     6
     5     6     7

Generalized, hankel output specified exactly:

w = floor(numel(x)/2);
X = hankel(x(1:end-w),x(w+1:end))

OTHER TIPS

A slightly contrived way using meshgrid:

k = (length(x) + 1) / 2;
[a b] = meshgrid(1:k, 0:k-1);
y = x(a+b);

Or the compact equivalent using bsxfun

y = x(bsxfun(@plus, (1:k)', 0:k-1));

Or a really silly one-liner:

y = x(interp2([1 3], [1;3], [1 3; 3 5], 1:3, (1:3)'));

You can do this the following way without direct loop:

cell2mat(arrayfun(@(k) x(k:k+2), 1:numel(x) - 2, 'UniformOutput', false)')

ans =

     1     3     5
     3     5     6
     5     6     7

Though, arrayfun actually loops over elements 1:numel(x) - 2. So its a bit of cheating, i guess.

Using convolutions:

n = numel(x)-2; %// x is a row vector with arbitrary length
result = conv2(x,rot90(eye(n)));
result = result(:,n:end-n+1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top