フィルターのクリア

array of natural numbers from 1 to n subset

60 ビュー (過去 30 日間)
Sofija
Sofija 2012 年 9 月 21 日
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
  1 件のコメント
Sofija
Sofija 2012 年 9 月 21 日
I'm introducing myself to matlab:) Thank you very much Tom!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by