array of natural numbers from 1 to n subset

144 views (last 30 days)
I have array of natural numbers from 1 to n. They are divided into m groups with m-1 elements -> m*(m-1)=n. I need to make n/2-length array whose elements are all 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: x=[1:20] I need 1,2,3,4; 6,7,8; 11,12; 16; Thanks!
  2 Comments
Thomas
Thomas on 21 Sep 2012
Sounds like homework.. what have you done so far?
Sofija
Sofija on 21 Sep 2012
Edited: Walter Roberson on 21 Sep 2012
well...nothing so far:)
only array [1 2 3 4 2 3 4 3 4 4]
unique=zeros(1,l/2);
ind=1;
for i=1:k-1
for j=1:(k-1-i+1)
unique(ind)=j+i-1;
ind=ind+1;
end
end
% code
end

Sign in to comment.

Accepted Answer

Thomas
Thomas on 21 Sep 2012
Edited: Thomas on 21 Sep 2012
Ok, there are multiple ways to go about, I'll suggest a start to one, but you will have to develop the logic yourself
First step: Create your array of n natural numbers for Eg
n=20;
your_array=[1:n] % create your array
m=5; % number of elements in groups
To split the array into subgroups, you might have to use for loops. Or oyu could just split the array using indexing into groups of m or (m-1) as you need. http://www.mathworks.com/company/newsletters/articles/Matrix-Indexing-in-MATLAB/matrix.html I'm showing the example with the reshape command, your output matrix to work with will be ( I for one, know when my students have used outside help when I see the reshape command used in an introduction to matlab code.. :))
a=reshape(your_array,m,n/m)'
Now you need the m-1 elements from row 1, m-2 from row 2 and so on..
a(1,1;m-1) % this is only for first row
Develop your logic for the completion of the code
  1 Comment
Sofija
Sofija on 21 Sep 2012
I'm introducing myself to matlab:) Thank you very much Tom!

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!