array of natural numbers from 1 to n subset

46 次查看(过去 30 天)
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 个评论
Thomas
Thomas 2012-9-21
Sounds like homework.. what have you done so far?
Sofija
Sofija 2012-9-21
编辑:Walter Roberson 2012-9-21
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

请先登录,再进行评论。

采纳的回答

Thomas
Thomas 2012-9-21
编辑:Thomas 2012-9-21
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by