Matlab code natural numbers subset

Click For Summary
SUMMARY

The discussion focuses on generating a subset of natural numbers divided into groups based on the equation m*(m-1)=n, where m represents the number of groups. The user seeks to extract specific elements from each group, specifically m-1 elements from the first group, last m-2 from the second, and so on, culminating in zero elements from the last group. A brute force solution is provided using MATLAB code, which utilizes cell arrays to accommodate the varying sizes of the resulting subsets.

PREREQUISITES
  • Understanding of MATLAB programming language
  • Familiarity with cell arrays in MATLAB
  • Knowledge of array indexing in MATLAB
  • Basic concepts of natural numbers and grouping
NEXT STEPS
  • Explore advanced MATLAB array manipulation techniques
  • Learn about MATLAB cell arrays and their applications
  • Investigate optimization strategies for MATLAB code
  • Study combinatorial mathematics related to grouping and subsets
USEFUL FOR

Mathematics enthusiasts, MATLAB programmers, and anyone interested in algorithm development for grouping natural numbers.

martika
Messages
2
Reaction score
0
I have array of natural numbers from 1 to n.
They are divided into m groups, where m*(m-1)=n.
I need all m-1 elements from first group, last m-2 elements from second group, last m-3 elements from third group...zero elements from last group.
For example
5*4=20:
1,2,3,4; 5,6,7,8; 9,10,11,12; 13,14,15,16; 17,18,19,20;.
I need 1,2,3,4; 6,7,8; 11,12; 16;
Thanks!
 
Physics news on Phys.org
Here is a brute force, inelegant, solution to your problem using cell arrays to hold to the final values since they are not equal in size. Do with it as you will :p

Code:
m = 5;
n = m*(m-1);
numbers = 1:n;

for i = 1:m
    
    j = i-1;
    
    group(i,:) = numbers((m-1)*j+1:(m-1)*i);
    
    group_ends{i} = group(i,i:end);
    
end
 
Thank you!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K