You can do this with upper and lower triangular parts of the matrix (triu and tril).
Then it's a 1 line solution:
B = [tril(A,-1) zeros(N, 1)] + [zeros(N,1) triu(A)];
Edit: benchmark
This is a comparison of the loop method, the 2 methods in Sardar's answer, and my method above.
Benchmark code, using timeit for timing and directly lifting code from question and answers:
function benchie() N = 1e4; A = rand(N,N-1); % Initialise large matrix % Set up anonymous functions for input to timeit s1 = @() sardar1(A,N); s2 = @() sardar2(A,N); w = @() wolfie(A,N); u = @() user3285148(A,N); % timings timeit(s1), timeit(s2), timeit(w), timeit(u) end function sardar1(A, N) % using eye as an indexing matrix B=double(~eye(N)); B(find(B))=A.'; B=B.'; end function sardar2(A,N) % similar to sardar1, but avoiding slow operations B=1-eye(N); B(logical(B))=A.'; B=B.'; end function wolfie(A,N) % using triangular parts of the matrix B = [tril(A,-1) zeros(N, 1)] + [zeros(N,1) triu(A)]; end function user3285148(A, N) % original looping method B = zeros(N,N); B(1,:) = [0 A(1,:)]; B(N,:) = [A(N,:) 0]; for j=2:N-1; B(j,:)= [A(j,1:j-1) 0 A(j,j:end)]; end end
Results:
- Sardar method 1: 2.83 secs
- Sardar method 2: 1.82 secs
- My method: 1.45 secs
- Looping method: 3.80 secs (!)
Conclusions:
- Your desire to vectorise this was well founded, looping is way slower than other methods.
- Avoiding data conversions and
find for large matrices is important, saving ~35% processing time between Sardar's methods. - By avoiding indexing all together you can save a further 20% processing time.