Reputation: 2570
I have a simple matrix: [3 5 9 10]. How can I transform it to: [3 0 ; 5 3 ; 9 5 ; 10 9]
[3 5 9 10]
[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
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
Reputation: 27017
a = 1:4; b = repmat(a',1,2); b(:,2) = b(:,2)-1;
Upvotes: 1