How to replace k-th diagonal by vector?
Show older comments
I have a code here that give me errors. In the code I am computing a i-th dot product and want to replace this values on the i-th super diagonal.
lambda=0.42;
n=100;
p=0.005;
P=zeros(1001,1001);
for i=1:1000
a=i:1000;
b=a-i;
v=poisspdf(a,lambda);
w=binopdf(b,n,p);
c=dot(v,w);
d=size(diag(P,i),1);%this is the size of the vector with elements of the kth diagonal
e=c*ones(d,1);
diag(P,i)=e;
end
But matlab gives me an error saying:
Subscript indices must either be real positive integers or logicals.
Error in pdfnumber (line 18)
diag(P,i)=e;
Then I tried a more simple code. But is only replaces the 1 super diagonal for me, while i runs from 1 to 1000.
for i=1:1000
a=i:1000;
b=a-i;
v=poisspdf(a,lambda);
w=binopdf(b,n,p);
c=dot(v,w);
P(i,i+1)=c;
end
What am I doing wrong?
Accepted Answer
More Answers (3)
Bruno Luong
on 3 Oct 2018
Edited: Bruno Luong
on 3 Oct 2018
>> P = rand(5,4)
P =
0.3517 0.2858 0.0759 0.1299
0.8308 0.7572 0.0540 0.5688
0.5853 0.7537 0.5308 0.4694
0.5497 0.3804 0.7792 0.0119
0.9172 0.5678 0.9340 0.3371
>> P(idiag(size(P),-1)) = (1:4)
P =
0.3517 0.2858 0.0759 0.1299
1.0000 0.7572 0.0540 0.5688
0.5853 2.0000 0.5308 0.4694
0.5497 0.3804 3.0000 0.0119
0.9172 0.5678 0.9340 4.0000
>>
Adam
on 2 Oct 2018
1 vote
P is defined as a matrix of all zeros and you appear to be overwriting the 'diag' function with a variable of the same name which you then try to index into using P. The entirety of P, in fact!
2 Comments
Clarisha Nijman
on 2 Oct 2018
Guillaume
on 2 Oct 2018
You cannot assign a value to a function, so
somefunction(atsomevalue) = something
is never correct in matlab. What you are attempting to do is assign a new value to the return value of the diag function, so if we'd made that explicit, your
diag(P, i) = e;
could be decomposed to:
tempvariable = diag(P, i);
tempvariable = e;
Even if matlab allowed that, you can see it still wouldn't work as you would only be changing the value of the temporary, not the diagonale of P.
Since assigning a value to a function is not allowed and since matlab allows you to use the same names for functions and variables, matlab interpret your diag line as an assigment of e to the matrix diag at rows P and column i. Some values in P are 0 which is not a valid row index, hence the error.
Clarisha Nijman
on 3 Oct 2018
0 votes
Categories
Find more on Operating on Diagonal 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!