Info

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

Vector with elements, reconstructable after addition...

1 次查看(过去 30 天)
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 个评论
Jan
Jan 2013-8-16
编辑:Jan 2013-8-16
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?

回答(0 个)

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by