Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
could anyone tell me how to replace the value of the given matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
A=[0.01 0.02 0.03 0 0 0 0.02;
0.02 0.03 0.04 0 0 0 0.03;
0.03 0.02 0.02 0 0 0 0.04]
if i add all the numbers it comes 0.31. Could anyone tell me how to replace the number present not the zeros in such a way that if i add the numbers in the matrix it should be 0.35
4 Kommentare
Stephen23
am 29 Jan. 2018
Bearbeitet: Stephen23
am 29 Jan. 2018
@Rik Wisselink: thank you for the concern. I actually want to help Prabha Kumaresan get a good solution... but it is not clear to me why Prabha Kumaresan is avoiding the use of the one function that easily solves this task. An explanation is what I am after!
Jan
am 29 Jan. 2018
Bearbeitet: Jan
am 29 Jan. 2018
@Stephen: Prabha does not give us explanations. It seems to be more a problem of the communication than of Matlab. Then this problem cannot be solved by communicating in the forum. We cannot help everybody, but all we can do is to suggest working solutions. We did this as good as we can, so it is time to be satisfied - even if the OP is not.
But driving crazy would be a reasonable reaction also. ;-)
Antworten (2)
Stephen23
am 29 Jan. 2018
Bearbeitet: Stephen23
am 29 Jan. 2018
>> A = [0.01 0.02 0.03 0 0 0 0.02; 0.02 0.03 0.04 0 0 0 0.03; 0.03 0.02 0.02 0 0 0 0.04];
>> A(A~=0) = randfixedsum(nnz(A),1,0.35,0.01,0.09)
A =
0.010084 0.024919 0.042718 0.000000 0.000000 0.000000 0.011433
0.011970 0.010413 0.011182 0.000000 0.000000 0.000000 0.055572
0.036536 0.016813 0.054944 0.000000 0.000000 0.000000 0.063416
>> sum(A(:))
ans = 0.35000
It is not clear why you are wasting so much of your time avoiding using randfixedsum.
0 Kommentare
the cyclist
am 26 Jan. 2018
A = 0.35*A/sum(A(:));
8 Kommentare
Jan
am 29 Jan. 2018
@Prabha: I do not see the problem.
A = [0.01 0.02 0.03 0 0 0 0.02; ...
0.02 0.03 0.04 0 0 0 0.03; ...
0.03 0.02 0.02 0 0 0 0.04]
A = 0.35 * A / sum(A(:));
Now sum(A(:)) is 0.35, exactly what you have asked for.
It is not useful to suggest another tool instead of randfixedsum, if this tool works perfectly.
Walter Roberson
am 29 Jan. 2018
The sum comes out as the closest representable floating point number to 35/100 which is the same floating point number that is used for the constant 0.35
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!