Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

could anyone tell me how to replace the value of the given matrix

1 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 29 Jan 2018
Edited: Stephen23 on 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
Jan on 29 Jan 2018
Edited: Jan on 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. ;-)

Answers (2)

Stephen23
Stephen23 on 29 Jan 2018
Edited: Stephen23 on 29 Jan 2018
This is trivially easy with randfixedsum, in just one line of code:
>> 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.

the cyclist
the cyclist on 26 Jan 2018
A = 0.35*A/sum(A(:));
  8 Comments
Jan
Jan on 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
Walter Roberson on 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

This question is closed.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!