Efficient way to make a multi-diagonal matrix - matlab

Is there an efficient way to produce square multi-diagonal matricies such as these:
[[1,2,3],
[2,1,2],
[3,2,1]]
[[1,2,3,4,5],
[2,1,2,3,4],
[3,2,1,2,3],
[4,3,2,1,2],
[5,4,3,2,1]]
My efforts so far have produced the following:
t=10
sum=zeros(t,t)
for i=1:t
sum+=diag(ones(1,i)*(t-i)+1,t-i);
end
sum
sum+sum'-diag(ones(1,10),0)

The command toeplitz does exactly what you want:
toeplitz([1,2,3,4,5,6])
ans =
1 2 3 4 5 6
2 1 2 3 4 5
3 2 1 2 3 4
4 3 2 1 2 3
5 4 3 2 1 2
6 5 4 3 2 1

What you are looking for is called a symmetric (Hermitian) toeplitz matrix.
I'm not familiar with matlab, but I found this documentation on mathworks:

Related

Alter number sort / number sequence

I have an array / number sequence a=[1,2,3,4,5] and I'm trying
to create an array / number sequence that looks like a_new below:
The columns represent the orders / index the numbers should go in.
a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]
My thoughts where to use circshift but quickly found out that would not work.
a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end
produces
a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]
Any thoughts? It doesn't need to use circshift if that won't work.
PS: I'm using Octave 4.2 which is similar to Matlab
There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat, toeplitz, tril, and triu:
>> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))
a_new =
1 2 3 4 5
2 1 2 3 4
3 3 1 2 3
4 4 4 1 2
5 5 5 5 1
I'm not sure about a built-in function, but this should work;
a=[1,2,3,4,5];
a_out = ones(length(a), length(a))
for n=1:5
a_out(n,:) = [n*ones(n-1),a(n:end)]
end
I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!
You can use spdiags to generate the matrix:
n = numel(a);
a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);

Remove zeros from matrix in matlab

I have a question and I hope it is not duplicate.
First, I have let say the following matrix:
A=[2 2 2 0 0
1 2 3 0 0
4 5 7 2 0]
I want to remove the zeros from A and return:
A=[2 2 2
1 2 3
4 5 7]
When I do
A(A==0)=[]
I get
A=[2 2 2 1 2 3 4 5 7]
Second, if instead of zeros I want to remove the elements that are greater than something. For example if I want to remove all elements greater than 6 (>6) of the following matrix B:
B=[2 2 2 5 3
1 2 3 6 8
4 5 7 2 1]
I get
A=[2 2 5
1 2 6
4 5 2]
P.S. I know how to do it using loops.
First problem solution
A(:,find(all(A,1)))
Second problem solution
B(:,~any(B>6,1))

How to duplicate all inner columns of a matrix and sum pairs of columns in Matlab

Suppose I have a matrix A
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
How do I duplicate the inner columns of A to get a new matrix B
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
1 2 2 3 3 4 4 5
Notice the first and last column of A were left alone. Then I need to sum pairs of rows together to get another matrix C:
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
The size of my matrices will not always be 5x5 and the elements will not always be so nice, but the matrix will always be square.
I do not need to generate or output matrix B. That was just simply how I initially thought of obtaining my final matrix C.
My goal is to be reasonably efficient, so I would like to accomplish this without a for loop.
How do I accomplish this for arbitrary matrix size nxn ?
Very simple . .
C = A(:,2:end) + A(:,1:end-1)

Get first two maximum values in matrix

I've got a matrix (n x m). And I'd like to know, for each row, the indexes of the coloums that contain the first two maximum values:
2 3 4 2
2 4 7 1
1 1 2 4
5 5 9 6
1 4 2 1
9 8 1 2
The answer should be:
2 3
2 3
3 4
3 4
2 3
1 2
How can I obtain it with matlab commands? I'd like not to use for loops. I tried with:
[x,y]=max(matrix')
y=y';
y gives me the colum indexes for the maximum elements. Now I'd set to zero these elements and repeat the instructions but I have no idea how to do. I treid:
matrix(:,y)=0;
but it doesn't work.
if A is your matrix, then sort and pick the top two indices,
[a ix]=sort(A,2)
ans= ix(:,end-1:end)

how can i create a matrix using vector with lag using matlab

vector=
3
4
8
5
2
1
6
the matrix i want to create is
3 0 0
4 3 0
8 4 3
5 8 4
2 5 8
1 2 5
6 1 2
This was already covered in one of your previous questions. Although you aren't trying to create a square matrix in this example, you can still use the TOEPLITZ function like in Jonas' answer:
M = toeplitz(vector,[vector(1) 0 0]);