Organizational Research By

Surprising Reserch Topic

Experts Most Trusted 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!
    

asked Sep 7, 2015 by rajesh
0 votes
2 views



Related Hot Questions



Government Jobs Opening


...