Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Vector with elements, reconstructable after addition...

1 vue (au cours des 30 derniers jours)
Anon
Anon le 16 Août 2013
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hi,
ok, you will notice that I am not very proficient in mathematics, so please excuse my lack of terminology in asking this question.
I want to write a function that takes a positive integer n and that outputs a vector with n elements. The elements in this vector should be reconstructable if only the sum of specific elements in this vector is known. Let me give an example:
function v = fun(n)
v = 2.^(1:n);
end
e.g., for n = 5 v = [2 4 8 16 32]
Now if I had the number a = 20 I can be sure that this number is an addition of v(ix) where ix = [2 4]. If a = 14, ix = [1 2 3]. I have no proof for this, but I guess that this will work out for larger n, too. However, there are two reasons why I run into trouble with this. First, if n is getting too large, I'll probably get a floating point issue. Second, I have no idea how to efficiently code a function that would return ix.
Any ideas, hints, etc. are very much appreciated. Please feel free to alter the question title.
  1 commentaire
Jan
Jan le 16 Août 2013
Modifié(e) : Jan le 16 Août 2013
In your example you are actually looking for a binary representation of a. Maybe check dec2bin. This gives a string having a 1 at the positions you are looking for.
i.e.
dec2bin( 20 ) = '10100'
and therefore 20 = 1*2.^4 + 0 * 2.^3 + 1 * 2.^2 + 0 * 2.^1 + 0 * 2.^0
Maybe this gets you started?

Réponses (0)

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by