You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How I can find a code to find (m) number form (1, n) and their sum must be different at each time.
1 view (last 30 days)
Show older comments
Ex : if the n = 5 and m = 4, which mean I want 4 number from (1,5) . The numbers appear are 1,2,3,4 and the sum is : 1+2=3 1+3=4 1+4=5 2+3=5 2+4=6 3+4=7 Clearly that the sum 5 is repeated so these number are rejected.
Accepted Answer
Image Analyst
on 25 Sep 2021
Edited: Image Analyst
on 25 Sep 2021
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?
20 Comments
Rami Ahmed
on 25 Sep 2021
What I need is no repeated sum which mean when I sum the number that appear the sum must be diiferent, 5 as a sum was repeated
Image Analyst
on 25 Sep 2021
You can use ismember() or unique():
n = 5;
m = 4;
numbers = linspace(1, n-1, m) % 4 numbers from 1 to m-1 (4)
index = 1;
theSum = [];
for k = 1 : m
num1 = numbers(k);
for k2 = k+1 : m
num2 = numbers(k2);
if index == 1 || ~ismember(num1+num2, theSum)
theSum(index) = num1 + num2;
fprintf('%.1f + %.1f = %f\n', num1, num2, theSum(index));
index = index + 1;
end
end
end
Image Analyst
on 25 Sep 2021
You said "sum all these number with each other". That means you use the sum() function. So what do we put into the sum function? All these numbers. Well, all the numbers that were the result of adding two numbers are in theSum. So I put theSum vector into sum. Does it make sense now? Otherwise give explicit description of what "all these number" means to you.
Rami Ahmed
on 25 Sep 2021
Edited: Rami Ahmed
on 25 Sep 2021
Its means the numbers that appear when I run, I want to generate numbers when I sum any of these numbers with any other numbers that appear the sum must be new, which means no sum repeated if I do it for all numbers that appear.
But I can't exclude any sum, 4 numbers mean 6 different sums.
Please if you can help me...
Image Analyst
on 25 Sep 2021
Edited: Image Analyst
on 25 Sep 2021
Your output in the original post gave 6 sums but then you said 5 was repeated (1+4 and 2+3) so you did not want to include that (I exclude the second pair that gives a repeat). That leaves 5 sums, not 6. My code provides the 5 sums.
1.0 + 2.0 = 3.000000
1.0 + 3.0 = 4.000000
1.0 + 4.0 = 5.000000
2.0 + 4.0 = 6.000000
3.0 + 4.0 = 7.000000
What is the missing 6th sum?
This isn't your homework is it? Because I'm not allowed to give you homeworls solutions where you could turn in my code as your own.
Rami Ahmed
on 25 Sep 2021
Edited: Rami Ahmed
on 25 Sep 2021
The wrong is with the result appear when I run the code, where the numbers must be 1,2,3,5 these numbers have 6 differnet sum. 1+2=3, 1+3=4, 1+5=6, 2+3=5, 2+5=7, 3+5=8,
Image Analyst
on 25 Sep 2021
So now they go out to 5 not 4? Did you change m or n?
Is m always n-1 or can it be any integer at all?
What happened to 1+4, 2+4, 3+4, and 4+5? Why are they not to be included?
Rami Ahmed
on 25 Sep 2021
Edited: Rami Ahmed
on 25 Sep 2021
No, m =4 and n=5 that means 4 numbers from 1to 5 I want to choose, and m can be any integer but must be less than n. I didnt choose 4 becsuse I want 4 numbers from 1 to 5 so I choose 1,2,3,5 and the sum of 4 with other numbers not necessary or I dont need it..
Image Analyst
on 25 Sep 2021
So if you have 4 numbers from 1 to 5, inclusive, then m is
n = 5;
m = 4;
numbers = linspace(1, n, m)
numbers = 1×4
1.0000 2.3333 3.6667 5.0000
Is that what you want?
Image Analyst
on 25 Sep 2021
Which integers? Do you just want to round and take unique ones?
n = 5;
m = 4;
numbers = unique(round(linspace(1, n, m)))
numbers = 1×4
1 2 4 5
So 3 will not be done? Or do you have some other algorithm?
What is the use case for this? No more help until you say if it's homework or not.
Rami Ahmed
on 25 Sep 2021
Edited: Rami Ahmed
on 25 Sep 2021
I swear its not homework its personal issue I want to solve because It will help me in my school, because I'm a math teacher. Above 1+5=6 and 2+4=6 so 1,2,4,5 are rejected.
Image Analyst
on 25 Sep 2021
Try this:
n = 5;
m = 5;
numbers = linspace(1, n, m)
index = 1;
theSums = [];
for k = 1 : length(numbers)
for k2 = 1 : length(numbers)
num1(index) = numbers(k);
num2(index) = numbers(k2);
theSums(index) = num1(index) + num2(index);
fprintf('%.4f + %.4f = %.4f\n', num1(index), num2(index), theSums(index));
index = index + 1;
end
end
[uniqueSums, indexes] = unique(theSums)
% Extract only the unique numbers:
num1 = num1(indexes)
num2 = num2(indexes)
% Double check.
for k = 1 : length(indexes)
fprintf('%.4f + %.4f = %.4f\n', num1(k), num2(k), uniqueSums(k));
end
Image Analyst
on 26 Sep 2021
Edited: Image Analyst
on 26 Sep 2021
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 = 1×4
1.0000 2.3333 3.6667 5.0000
numbers = unique(round(linspace(1, n, m)))
numbers = 1×4
1 2 4 5
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)