how can I create a matrix from calculated answers
Show older comments
I have this answers and I want to put them in a diagonal matrix.Pls help me!
y =
0.0025 + 0.0155i
y =
0.0030 + 0.0171i
y =
0.0038 + 0.0191i
y =
0.0049 + 0.0216i
y =
0.0066 + 0.0248i
y =
0.0092 + 0.0289i
y =
0.0137 + 0.0344i
y =
0.0220 + 0.0414i
y =
0.0388 + 0.0487i
y =
0.0717 + 0.0450i
y =
1000000
y =
0.0717 - 0.0450i
y =
0.0388 - 0.0487i
y =
0.0220 - 0.0414i
y =
0.0137 - 0.0344i
y =
0.0092 - 0.0289i
y =
0.0066 - 0.0248i
y =
0.0049 - 0.0216i
y =
0.0038 - 0.0191i
y =
0.0030 - 0.0171i
y =
0.0025 - 0.0155i
2 Comments
James Tursa
on 26 Feb 2011
Can you show the code that generates these numbers? You can pre-allocate a result and then assign them to the appropriate spots in the result, or maybe there is a better vectorized solution, but we can't tell without seeing your code.
P
on 26 Feb 2011
Accepted Answer
More Answers (2)
P
on 26 Feb 2011
0 votes
Matt Tearle
on 26 Feb 2011
M = zeros(n);
for k = 1:n
% calculate y
M(k,k) = y;
end
Or, preferably, if you can calculate all the y values in a vector, as explained here you can then do
M(1:n+1:end) = y;
Edit: fixed typo ( n+1 instead of n )
4 Comments
P
on 26 Feb 2011
James Tursa
on 26 Feb 2011
Matt, I think you meant:
M(1:n+1:end) = y;
P
on 26 Feb 2011
Matt Tearle
on 27 Feb 2011
Oops, thanks James for catching the typo.
Categories
Find more on Creating and Concatenating 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!