Matlab code natural numbers subset

In summary, the conversation discusses a problem where a given array of natural numbers from 1 to n is divided into m groups, where m*(m-1)=n. The desired output is the first m-1 elements from the first group, the last m-2 elements from the second group, and so on until zero elements from the last group. An example is given for n=20 where the desired output is 1,2,3,4; 6,7,8; 11,12; 16. A brute force solution using cell arrays is proposed.
  • #1
martika
2
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
  • #2
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
 
  • #3
Thank you!
 

What is a natural number?

A natural number is a positive integer that is used for counting and ordering. It does not include fractions, decimals, or negative numbers.

What is a subset in Matlab?

A subset in Matlab is a smaller collection of elements that are taken from a larger set. It can be created by selecting specific elements from a larger array or matrix.

How can I generate a subset of natural numbers in Matlab?

To generate a subset of natural numbers in Matlab, you can use the colon operator and specify the range of numbers you want to include. For example, the code "1:5" will generate a subset of natural numbers from 1 to 5.

Can I perform mathematical operations on a natural numbers subset in Matlab?

Yes, you can perform mathematical operations on a natural numbers subset in Matlab. Since natural numbers are represented as integers in Matlab, you can use standard arithmetic operators such as addition, subtraction, multiplication, and division on them.

How can I check if a subset of natural numbers meets a specific condition in Matlab?

You can use logical indexing in Matlab to check if a subset of natural numbers meets a specific condition. For example, the code "subset(subset > 10)" will return all the numbers in the subset that are greater than 10.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
561
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Replies
5
Views
892
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
960
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top