简体   繁体   中英

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] [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!

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)].'
a = 1:4;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM