필터 지우기
필터 지우기

array of natural numbers from 1 to n subset

조회 수: 68 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by