Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

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

1 visualización (últimos 30 días)
Prabha Kumaresan
Prabha Kumaresan el 26 de En. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 comentarios
Stephen23
Stephen23 el 29 de En. de 2018
Editada: Stephen23 el 29 de En. de 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 el 29 de En. de 2018
Editada: Jan el 29 de En. de 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. ;-)

Respuestas (2)

Stephen23
Stephen23 el 29 de En. de 2018
Editada: Stephen23 el 29 de En. de 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 el 26 de En. de 2018
A = 0.35*A/sum(A(:));
  8 comentarios
Jan
Jan el 29 de En. de 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 el 29 de En. de 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

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by