how to make symmetric matrix with vector of its non repeated elements?
Show older comments
suppose we have non repeated elements of a matrix in a vector . I need to make a symmetric matrix by using this vector in matlab.
for example if we have :
v= [ 1 4 5 6 9 0]
the answer must be the matrix:
1 4 5
4 6 9
5 9 0
1 Comment
Luca Ferro
on 1 Mar 2023
could you better define the rules of the reshape?
Accepted Answer
More Answers (1)
Pratheek
on 1 Mar 2023
The first line of the code is to input for the size of the matrix that you want to generate.
% ask the user for the size of the matrix
n = input('Enter the size of the square matrix: ');
% get the input vector
v = [1 4 5 6 9 0]; % or prompt the user to enter the vector as well
% create the symmetric matrix
M = zeros(n);
k = 1;
for i = 1:n
for j = (i+1):n
M(i,j) = v(k);
M(j,i) = v(k);
M(i,i) = v(randi(length(v)));
k = k + 1;
end
end
% display the resulting matrix
disp(M);
1 Comment
Jan
on 1 Mar 2023
Why do you set the diagonal elements randomly and repeatedly in each iteration of the inner loop?
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!