Re: changing array dimensions [message #43184] |
Thu, 17 March 2005 11:46 |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
anu wrote:
> I have a problem or sorting out data from one array and putting them
> into individual arrays
>
> a = [1,2,1,1,2,3,3,3,4,4,3,2,1,2]
> i am able to sort out the numbers of same values... but i am not able
> to put them into separate arrays
>
> since : number of a[1] = 4 which is different from number a[4]=2
>
> so if i try putting it in a two dimensional array .. i get a lot of 0
> values which i dont want
>
> does anybody have an idea if it is possible to sort out data into
> single dimensional or two dimensional arrays?
Please explain further what you're trying to do and why.
It's true that you cannot have a 2-dimensional array in which the rows
have different lengths. That's a fundamental limitation of arrays. There
are data structures that you could use, if this is really what you want,
eg a 1-D array of pointers, each referencing a 1-D array of values. But
then, are you saying each of these value of arrays would be filled with
the same value, repeated according to its number of occurrences in the
original array? That seems like a waste of space. What do you really
want to know? How many times each value occurs? Where each occurrence
is? How many different values there are and what they are?
You can use the SORT function to sort your array and you can use SORT
together with UNIQ to return a list of the unique values--see the
documentation for both these functions.
You can use HISTOGRAM to count the occurrences of each value. Actually,
you can do a great deal more with HISTOGRAM, see
http://www.dfanning.com/tips/histogram_tutorial.html
It all depends on what you're *really* trying to do.
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|