electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
Writing to multidimensional arrays -- 2d array into 3d array
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
fingerfunk



Joined: Oct 31, 2012
Posts: 2
Location: Los Angeles, CA

PostPosted: Wed Oct 31, 2012 6:54 pm    Post subject: Writing to multidimensional arrays -- 2d array into 3d array
Subject description: Possible to chuck a 2D array into a specific location on third dimension of 3D array?
Reply with quote  Mark this post and the followings unread

Hi everybody. I'm new to this forum, this is my first post. I've found a few helpful posts about ChucK here -- thanks. I'm relatively new to ChucK also (have been learning it for a couple months), and programming in general.

My question concerns multidimensional arrays (3D arrays, specifically). I'm trying to better understand how write to them.

My basic question is: is it possible to chuck a 2D array to a specific location along the third dimension (i.e. not either of the two dimension of the 2D array being chucked) of a 3D array?

I was having trouble understanding how to write to / access different dimensions of a 3D array, so I did some tests to figure it out. Here's my test:

Code:

[ [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
[17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],

[[33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48],
[49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ] @=> int A[][][];

<<<"A[0][0][0]: ",A[0][0][0]>>>;
<<<"A[0][1][0]: ",A[0][1][0]>>>;
<<<"A[0][1][1]: ",A[0][1][1]>>>;

<<<"A[1][0][0]: ",A[1][0][0]>>>;
<<<"A[1][1][0]: ",A[1][1][0]>>>;
<<<"A[1][1][1]: ",A[1][1][1]>>>;

150 @=> A[1][2][0];

<<<"A[1][2][0]: ",A[1][2][0]>>>;



The program prints the following:

Code:

[chuck](VM): sporking incoming shred: 1 (3D matrix tests.ck)...
A[0][0][0]:  1
A[0][1][0]:  17
A[0][1][1]:  18
A[1][0][0]:  33
A[1][1][0]:  49
A[1][1][1]:  50
A[1][2][0]:  150



So far so good. I understand how to instantiate the array in 3 dimensions. I understand how chuck refers to the different positions in the array. I understand how to read specific location in the array. And I understand how to write to the array, two different ways. The way I think of the first method used here is that I'm sending a couple of different 2D arrays to a 3D array. The second method sends a specific value to a specific location in the array.

But what if, later on, I want to send another 2D array to the 3D array? Is there a way to reference a specific position along the 3rd dimension (or 1st dimension, however you think about it), and send a 2D array to it? I can't seem to figure anything out.

I tried the following:

Code:

[[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],
[81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] @=> A[2][][];


But I get a syntax error on the last line.

I also tried this:

Code:

[[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],
[81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] @=> A[2][0][0];


But that produces the following error:

Code:

[3D matrix tests.ck]:line(63): cannot assign '@=>' on types 'int[][]' @=> 'int'...
[3D matrix tests.ck]:line(63): ...(reason: --- incompatible types for assignment


Any ideas? Is it possible to do what I'm trying to do? Am I making sense?

Thanks in advance!
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 3720
Location: Sweden
Audio files: 273
G2 patch files: 96

PostPosted: Thu Nov 01, 2012 12:05 am    Post subject: Reply with quote  Mark this post and the followings unread

Instead of "@=> A[2][][]" write "@=> A[2]".
_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
Antimon



Joined: Jan 18, 2005
Posts: 3720
Location: Sweden
Audio files: 273
G2 patch files: 96

PostPosted: Thu Nov 01, 2012 12:07 am    Post subject: Reply with quote  Mark this post and the followings unread

You can see a 3d matrix as an array of matrixes, a matrix of arrays, or an array of arrays of arrays. When you are writing the 2d matrix, you are really just storing an element in the "outermost" array.
_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
fingerfunk



Joined: Oct 31, 2012
Posts: 2
Location: Los Angeles, CA

PostPosted: Thu Nov 01, 2012 1:29 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks! Your solution didn't seem to work for me though. I tried it and got an "array out of bounds" message.

Here's the entire test program:

Code:

[ [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
[17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],

[[33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48],
[49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ] @=> int A[][][];


<<<"A[0][0][0]: ",A[0][0][0]>>>;
<<<"A[0][1][0]: ",A[0][1][0]>>>;
<<<"A[0][1][1]: ",A[0][1][1]>>>;

<<<"A[1][0][0]: ",A[1][0][0]>>>;
<<<"A[1][1][0]: ",A[1][1][0]>>>;
<<<"A[1][1][1]: ",A[1][1][1]>>>;

150 @=> A[1][2][0];

<<<"A[1][2][0]: ",A[1][2][0]>>>;


[[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80],
[81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] @=> A[2];


Here's what it printed:

Code:

[chuck](VM): sporking incoming shred: 1 (3D matrix tests.ck)...
A[0][0][0]:  1
A[0][1][0]:  17
A[0][1][1]:  18
A[1][0][0]:  33
A[1][1][0]:  49
A[1][1][1]:  50
A[1][2][0]:  150
[chuck](VM): ArrayOutofBounds: in shred[id=1:3D matrix tests.ck], PC=[896], index=[2]


Any thoughts? Conceptually, your description of how multidimensional arrays work is making sense to me. But the implementation you suggested isn't working (unless I'm missing something).
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 3720
Location: Sweden
Audio files: 273
G2 patch files: 96

PostPosted: Thu Nov 01, 2012 5:46 am    Post subject: Reply with quote  Mark this post and the followings unread

Yeah sorry, to write to the third element (index 2) in the first dimension (root array) you have to allocate memory for it first, or use the dynamic array stuff that increases the size of the array as you add stuff to it. I can't do the research now, but I think there are examples for dynamically adding array elements shipped with ChucK.

The alternative is to allocate enough memory at the start. I'm not sure, but you might be able to change the last line for setting up A to:

Code:
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
[] ] @=> int A[][][];


adding an empty array that you can replace by writing to A[2] later. Or try initalizing A like this:

int A[3][][];

(Edit: i.e. don't set the contents next to the matrix declaration using @=>, set them later)

and then write each matrix to A[0], A[1] and A[2].

I haven't compiled this, but it should work. Experiment and look at the stuff in ChucK's examples folder. :)

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
e-m mkii

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use