Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2018-1-29
编辑:Stephen23 2018-1-29
@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 2018-1-29
编辑:Jan 2018-1-29
@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. ;-)

回答(2 个)

Stephen23
Stephen23 2018-1-29
编辑:Stephen23 2018-1-29
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 2018-1-26
A = 0.35*A/sum(A(:));
  8 个评论
Jan
Jan 2018-1-29
@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 2018-1-29
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

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by