Filter löschen
Filter löschen

can someone help me create function with 2 inputs that creates a single output?

8 Ansichten (letzte 30 Tage)
I have been asked to create the following:
Create a function called Ex7b with 2 inputs (n and m) and a single output. Write the function so the output is the sum of the sets of n numbers between 1 and m. for example if n=4 and m=100:
?????? = [1 + 2 + 3 + 4 5 + 6 + 8 + 9......97 + 98 + 99 + 100]. The function should round m to the nearest multiple of n.
I have looked at all the info on creating functions, but have no idea on where to start with this one.
Thanks

Akzeptierte Antwort

Stephan
Stephan am 1 Mär. 2019
Hi,
start reading here and work through the examples given:
Best regards
Stephan
  4 Kommentare
Sam Thorpe
Sam Thorpe am 4 Mär. 2019
Thank you for your help Stephan. After a bit of logical thinking and your tips, I have gotten the function to work exactly as I wanted.
function ex7b=sumset(n,m)
o=n*ceil(m/n) %round the array up to nearest multiplication of n
p=[1:o] %definition of the array
q=(numel(p))/n %calculation of column size
x=reshape(p,[n,q]) %reshaping the array in to an n by m/n array
y=x.' %transposition of array
z=sum(y) %sum of each array row
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by