Could anyone help me to get the sum of an array to a fixed value
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
A=[1 2 3 4;
5 6 7 8]
how to get the sum of A to be fixed to a
value of 20 such that all the values in A needs
to be changed according to it.
3 commentaires
Réponses (3)
KSSV
le 23 Oct 2018
A=[1 2 3 4;
5 6 7 8] ;
A = A(:) ;
iwant = cell([],1) ;
count = 0 ;
for i = 1:length(A)
B = nchoosek(A,i) ;
thesum = sum(B,2) ;
idx = thesum==20 ;
if any(idx)
count = count+1 ;
iwant{count} = B(idx,:) ;
end
end
iwant
Bruno Luong
le 23 Oct 2018
"There is no fixed logic"
OK that's easy then
A(:) = 0;
A(1) = 20;
3 commentaires
Kevin Chng
le 23 Oct 2018
Modifié(e) : Kevin Chng
le 23 Oct 2018
How about
A(:)=1;
A(1) = 20-sum(A(2:end));
provided number of element in A lesser than 20.
Bruno Luong
le 23 Oct 2018
Modifié(e) : Bruno Luong
le 23 Oct 2018
Let's be more serious you can do many thing like shifting
A = A - sum(A) + 20/size(A,1);
or scaling
A = 20 * A ./ sum(A);
or both
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!