Remember
Register
All Activity
Tags
Users
Ask a Question
About Boostr
Advertise on boostr.in
Jobs
Organizational Research By
Surprising Reserch Topic
working on diagonal matrix
working on diagonal matrix Â using -'matlab,matlab-guide'
I have a vector v of size 1 x 5, Â and a diagonal matrix D of size 5 x 5. In my example, I have v = [0 1 2 3 4].
First question: I want to put the vector v on the diagonal of D, so that D(1,1) = 0, D(2,2) = 1, D(3,3) = 2, D(4,4) = 3 and D(5,5) = 4.
I wrote a matlab code for this but I am sure that there is another automatic method much less expensive in computation.
So what I wrote (and to be optimized from you) is the following:
Â Â Â Â ii = 1;
Â Â Â Â for a = 1 : size(D,1)
Â Â Â Â for b = 1 : size(D,2)
Â Â Â Â if(a == b)
Â Â Â Â D(a,b) = v(1, ii);
Â Â Â Â ii = ii + 1;
end
end
end
Second Question: After finishing the first question, I need now to check if the diagonal values of D are equal to zero. If I can find (in an automatic way) a value on the diagonal of D is equal to zero, so replace it by 0.001.
In fact this can be written in a lot of manners, for example:
for a = 1 : size(D,1)
for b = 1 : size(D,2)
if(a == b)
if(D(a,b) == 0)
D(a,b) = 0.001;
end
end
end
end
But I need another automatic method which can be very cheap in terms of computations.
Any help will be very appreciated!
Â Â Â Â
matlab
matlab-guide
asked
Sep 7, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
2
views
Related Hot Questions
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or
register
.
...