1

It's like a reverse version of Pascal's triangle. I want to create a vector-input function named y = lastnum(vect) on Octave, that evaluate the sum of each pair of numbers in any vectors to output the single number from the evaluation loops like this

0 1 2 3 4 1 3 5 7 4 8 12 12 20 32 

And the input and output would be like this,

lastnum([0 1 2 3 4]) ans = 32 

I mean... is there any progresses that I can do??? You may not understand but, the reverse triangle above can guide you about my question.

I also tagged MATLAB since it has similar language. MATLAB pros may help my problem.

3 Answers 3

4

Notice that the number of times each element gets added to produce the final result comes from Pascals triangle itself, so, e.g., for the vector [a b c d] the result will be a+3b+3c+d. So create a vector of entries in Pascals triangle and multiply and add with the original vector v.

I only have access to Matlab, Octave may not have all these functions.

This is a one-liner diag(fliplr(pascal(numel(v)))).'*v(:).

Or a looping version

s = 0; for i = 0:numel(v)-1 s = s+nchoosek(numel(v)-1,i)*v(i+1); end s 
Sign up to request clarification or add additional context in comments.

Comments

2

Simplest thing I can think of is:

while length(x) > 0 disp(x) x = x(1:end-1) + x(2:end); end 

or did I misunderstand the question?

1 Comment

This is how I interpreted the question too.
1

Here is the version for both MATLAB and Octave:

function y = lastnum(v) while 1 if length(v) == 2 y = sum(v) break; end # disp(v); # if you want to print the progress vt = []; for k = 1:(length(v)-1) vt(end+1) = sum(v(k:(k+1))); end v = vt; end end 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.