87 views (last 30 days)

Show older comments

Image Analyst
on 25 Sep 2021 at 14:01

Edited: Image Analyst
on 25 Sep 2021 at 14:04

I know it seems obvious but did you try a simple for loop? I assume you did and got something like

n = 5;

m = 4;

numbers = linspace(1, n-1, m) % 4 numbers from 1 to m-1 (4)

for k = 1 : m

num1 = numbers(k);

for k2 = k+1 : m

num2 = numbers(k2);

fprintf('%.1f + %.1f = %f\n', num1, num2, num1+num2);

end

end

numbers =

1 2 3 4

1.0 + 2.0 = 3.000000

1.0 + 3.0 = 4.000000

1.0 + 4.0 = 5.000000

2.0 + 3.0 = 5.000000

2.0 + 4.0 = 6.000000

3.0 + 4.0 = 7.000000

but I'm wondering why this was no good. Did you need something more "sophisticated" for some reason?

Image Analyst
on 26 Sep 2021 at 14:39

Again, you can call round(). But you must realize that you cannot divide a range of n long into m segments without the segments being fractional, except for certain special cases. Like I said:

n = 5;

m = 4;

numbers = linspace(1, n, m)

numbers = unique(round(linspace(1, n, m)))

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

Start Hunting!