Azzera filtri
Azzera filtri

array of natural numbers from 1 to n subset

60 visualizzazioni (ultimi 30 giorni)
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 Commenti
Thomas
Thomas il 21 Set 2012
Sounds like homework.. what have you done so far?
Sofija
Sofija il 21 Set 2012
Modificato: Walter Roberson il 21 Set 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

Accedi per commentare.

Risposta accettata

Thomas
Thomas il 21 Set 2012
Modificato: Thomas il 21 Set 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 Commento
Sofija
Sofija il 21 Set 2012
I'm introducing myself to matlab:) Thank you very much Tom!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by