Maddy
Maddy

Reputation: 2570

Matrix manipulation in MATLAB

I have a simple matrix: [3 5 9 10]. How can I transform it to: [3 0 ; 5 3 ; 9 5 ; 10 9]

I tried using hankel etc. but that did not work. This needs to be a vector operation (no for/while loop). Thanks!

Upvotes: 2

Views: 253

Answers (2)

gnovice
gnovice

Reputation: 125854

You were close. You actually want to use the function TOEPLITZ instead:

>> vec = [3 5 9 10];
>> toeplitz(vec,[vec(1) 0])

ans =

     3     0
     5     3
     9     5
    10     9

However, since you only have 2 columns in your matrix, this is much simpler:

[vec; 0 vec(1:end-1)].'

Upvotes: 4

eykanal
eykanal

Reputation: 27017

a = 1:4;

b = repmat(a',1,2);
b(:,2) = b(:,2)-1;

Upvotes: 1

Related Questions