I am trying to get a 3*3 diagnol matrix by using the following code. But the resultant matrix dimensions exceed 3*3 matrix.

조회 수: 1 (최근 30일)
theta = 0:pi:2*pi;
a = cos(theta)+sin(theta);
A = ismatrix([[a,0,0],[0,a,0],[0,0,a]]);
a = cos(theta)+sin(theta);
A = ismatrix([[a,0,0],[0,a,0],[0,0,a]]);
B = [a 0 0;0 a 0;0 0 a];
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2018년 5월 6일
Thanks for the response, what i want to do is to get set of numbers in the matrix while keeping size of the matrix same. for example: a = 0:2*pi b = cos(a)+sin(a) c = [b 0 0;0 b 0;0 0 b] I can use diag to get the response keeping the matrix dimension same but it only gives me the final values. for theta = 0:2*pi a = cos(theta)+sin(theta); c = [a 0 0;0 a 0;0 0 a] end doing so gives me the result but i get number of matrices with each values of theta but the workspace stores just the final matrix.
Stephen23
Stephen23 2018년 5월 6일
편집: Stephen23 2018년 5월 6일
@Ameer Hamza: The point of my comment is that your code does not create a matrix.

댓글을 달려면 로그인하십시오.

답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 5월 6일
편집: Ameer Hamza 2018년 5월 6일
If you want the element of a to be the diagonal of B, you need something like this
theta = 0:pi:2*pi;
a = cos(theta)+sin(theta);
B = diag(a);
  댓글 수: 4
Ameer Hamza
Ameer Hamza 2018년 5월 6일
Thank you. My problem is that Matlab dont store all the values of matrix a , it store only the final value. I tried the method in this video https://blogs.mathworks.com/videos/2007/08/20/matlab-basics-video/ but it increase the dimension of the matrix. I need only (3*3) matrix.
Ameer Hamza
Ameer Hamza 2018년 5월 6일
In that video, for loop is used. In my code there is not for loop and MATLAB does store all the values of a. Have you tried running the code? The following code gives a vector (containing all values of a)
theta = 0:pi:2*pi;
a = cos(theta)+sin(theta);
disp(theta);
ans =
0 3.1416 6.2832
disp(a);
ans =
1 -1 1
You still did not explain, what do you want in MATRIX?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by