Can I assign a variable to a number in a matrix from another matrix?

2 次查看(过去 30 天)
Using the matrix that comes from eig(), I would like to assign individual values in it to 3 separate variables, and then plug those into a separate matrix. However, I get an error. How do I do this?
E = eig(A)
E(1,1) = w1;
E(2,1) = w2;
E(3,1) = w3;
ans = [m1*w1^2 0 0;
0 m2*w2^2 0;
0 0 m3*w3^2 0]
Unrecognized function or variable 'w1'.
Error in eigen (line 23)
E(1,1) = w1;

采纳的回答

Walter Roberson
Walter Roberson 2021-3-2
A = magic(7)
A = 7×7
30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20
E = eig(A)
E = 7×1
175.0000 -56.4848 -31.0882 -25.3967 56.4848 25.3967 31.0882
w1 = E(1,1)
w1 = 175
w2 = E(2,1)
w2 = -56.4848
w3 = E(3,1)
w3 = -31.0882
m1 = 3; m2 = 11; m3 = -7;
output = [
m1*w1^2 0 0;
0 m2*w2^2 0;
0 0 m3*w3^2]
output = 3×3
9.1875 0 0 0 3.5096 0 0 0 -0.6765
  6 个评论
Anastasia Zistatsis
I apologize for the weight, here you go @Walter Roberson
m1 = 1;
m2 = 1;
m3 = 1;
k = 2;
A = [2*k -k -k;
-k 2*k -k;
-k -k 2*k];
E = eig(A)
w1 = E(1,1)
w2 = E(2,1)
w3 = E(3,1)
output = [m1*(w1)^2 0 0;
0 m2*(w2)^2 0;
0 0 m3*(w3)^2]
Walter Roberson
Walter Roberson 2021-3-11
syms k
A = [2*k -k -k;
-k 2*k -k;
-k -k 2*k];
E = eig(A)
E = 
If you are expecting 2.449 then your k would have to be roughly 0.816

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2021-3-2
There's a way to do this without creating 2*n individual variables.
A = magic(7);
E = eig(A);
w = E(1:3);
m = [3; 11; -7];
output1 = diag(m.*(w.^2))
output1 = 3×3
9.1875 0 0 0 3.5096 0 0 0 -0.6765
w1 = E(1,1);
w2 = E(2,1);
w3 = E(3,1);
m1 = 3; m2 = 11; m3 = -7;
output2 = [ m1*w1^2 0 0;
0 m2*w2^2 0;
0 0 m3*w3^2]
output2 = 3×3
9.1875 0 0 0 3.5096 0 0 0 -0.6765
You can compare output1 and output2. The first block of code scales to larger matrices with more eigenvalues.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by