Thread Subject: Handling arrays

 Subject: Handling arrays From: William Date: 17 Nov, 2011 01:18:14 Message: 1 of 7 First post, I have a some knowledge of C, so I'm not completely wet behind the ears, but I am new to MATLAB. I have some arrays of integers that are different sizes i.e. function E = five(nx_min,nx_max,ny_min,ny_max,nz_min,nz_max) nx=[nx_min:1:nx_max]; ny=[ny_min:1:ny_max]; nz=[nz_min:1:nz_max]; Basically what I want 'E' to be is the sum of all the possible combinations of nx, ny, and nz. So if nx = 0:1 , ny =0:2, nz = 0:3, E should be.... E = nx + ny + nz E = 0 + 0 + 0 E = 0 + 0 + 1 E = 0 + 0 + 2 E = 0 + 1 + 0 E = 0 + 1 + 1 E = 0 + 1 + 2 E = 1 + 0 + 0 E = 1 + 0 + 1 E = 1 + 0 + 2 E = 1 + 1 + 0 E = 1 + 1 + 1 E = 1 + 1 + 2 In C I would just write a loop within a loop within a loop and start nx, ny, and nz at their lowest increment then index nz until the end of the array was reached, then step out of the inner most loop into the ny loop, increment it by 1, then run the nz loop and continue until all the incrementations has occurred. So here are my questions to help me solve my problem, unless another solution is easier. 1. How do I determine the size of an array to determine how many increments to make my loop? 2. How do I incrementally access data in an array, i.e. move from one cell to the next? 3. How do I add to an array? Basically I would like to all those E from those sums up there into one giant E array to save. I hope this all makes sense, if not, let me know if I can clarify at all.
 Subject: Handling arrays From: Roger Stafford Date: 17 Nov, 2011 03:05:29 Message: 2 of 7 "William" wrote in message ... > nx=[nx_min:1:nx_max]; > ny=[ny_min:1:ny_max]; > nz=[nz_min:1:nz_max]; > > Basically what I want 'E' to be is the sum of all the possible combinations of nx, ny, and nz. - - - - - - - - - -   Matlab already has a routine that accomplishes this. It is called 'ndgrid'. No need for 'for loops' here.   [Nz,Ny,Ny] = ndgrid(nz_min:nz_max,ny_min:ny_max,nx_min:nx_max);   E = Nz(:) + Ny(:) + Nx(:); Roger Stafford
 Subject: Handling arrays From: William Date: 17 Nov, 2011 03:40:28 Message: 3 of 7 "Roger Stafford" wrote in message ... > "William" wrote in message ... > > nx=[nx_min:1:nx_max]; > > ny=[ny_min:1:ny_max]; > > nz=[nz_min:1:nz_max]; > > > > Basically what I want 'E' to be is the sum of all the possible combinations of nx, ny, and nz. > - - - - - - - - - - > Matlab already has a routine that accomplishes this. It is called 'ndgrid'. No need for 'for loops' here. > > [Nz,Ny,Ny] = ndgrid(nz_min:nz_max,ny_min:ny_max,nx_min:nx_max); > E = Nz(:) + Ny(:) + Nx(:); > > Roger Stafford Thanks Roger, before you replied I figured out how to do it with loops! But thanks for the help and good to know for future reference. I'm having one other array issue though. I'm trying to pass an array of varying size to another array. Something simple like x = y; where y is an array of [1:x] dimensions. When I do this and look at y, is has the proper size [1:x] but only the first cell copies. Any suggestions?
 Subject: Handling arrays From: William Date: 17 Nov, 2011 03:44:29 Message: 4 of 7 "Roger Stafford" wrote in message ... > "William" wrote in message ... > > nx=[nx_min:1:nx_max]; > > ny=[ny_min:1:ny_max]; > > nz=[nz_min:1:nz_max]; > > > > Basically what I want 'E' to be is the sum of all the possible combinations of nx, ny, and nz. > - - - - - - - - - - > Matlab already has a routine that accomplishes this. It is called 'ndgrid'. No need for 'for loops' here. > > [Nz,Ny,Ny] = ndgrid(nz_min:nz_max,ny_min:ny_max,nx_min:nx_max); > E = Nz(:) + Ny(:) + Nx(:); > > Roger Stafford Also, I tired x=copy(y) but get the error "??? Undefined function or method 'copy' for input arguments of type 'sym'."
 Subject: Handling arrays From: dpb Date: 17 Nov, 2011 05:15:01 Message: 5 of 7 On 11/16/2011 9:40 PM, William wrote: ... > Thanks Roger, before you replied I figured out how to do it with loops! > But thanks for the help and good to know for future reference. Avoid loops in Matlab; otherwise, you're throwing away 90% of the reason for using it. > I'm having one other array issue though. > > I'm trying to pass an array of varying size to another array. Something > simple like x = y; where y is an array of [1:x] dimensions. When I do > this and look at y, is has the proper size [1:x] but only the first cell > copies. Any suggestions? What you mean by "pass an array to another array"???? If you want x=y; % where y exists, x is a copy that's all there is. If you mean to place the values in an array y into an existing array, in Matlab the target and source must be the same conformant in size in pure arrays. Non-rectangular or "ragged" arrays can only exist as cell arrays. Seems like working through the "Getting Started" chapter in the online doc's would be _a_good_thing_ (tm). --
 Subject: Handling arrays From: Steven_Lord Date: 17 Nov, 2011 14:24:39 Message: 6 of 7 "William " wrote in message news:ja1vnc\$qlk\$1@newscl01ah.mathworks.com... > "Roger Stafford" wrote in message > ... >> "William" wrote in message ... >> > nx=[nx_min:1:nx_max]; >> > ny=[ny_min:1:ny_max]; >> > nz=[nz_min:1:nz_max]; >> > >> > Basically what I want 'E' to be is the sum of all the possible >> > combinations of nx, ny, and nz. >> - - - - - - - - - - >> Matlab already has a routine that accomplishes this. It is called >> 'ndgrid'. No need for 'for loops' here. >> >> [Nz,Ny,Ny] = ndgrid(nz_min:nz_max,ny_min:ny_max,nx_min:nx_max); >> E = Nz(:) + Ny(:) + Nx(:); >> >> Roger Stafford > > Thanks Roger, before you replied I figured out how to do it with loops! > But thanks for the help and good to know for future reference. > > I'm having one other array issue though. > > I'm trying to pass an array of varying size to another array. Something > simple like x = y; where y is an array of [1:x] dimensions. When I do this > and look at y, is has the proper size [1:x] but only the first cell > copies. Any suggestions? I'm not quite sure I understand what you're asking. Could you post a few lines of your code, a small example of data that "look like" the data that you pass into your full code, and describe what you'd expect to see as a result using that specific code and sample data? -- Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com
 Subject: Handling arrays From: Christopher Creutzig Date: 22 Nov, 2011 09:37:12 Message: 7 of 7 On 17.11.11 04:44, William wrote: > Also, I tired x=copy(y) but get the error "??? Undefined function or method 'copy' for input arguments of type 'sym'." What did you expect that line to do? Creating a copy of y is as simple as x = y; also for y being a sym object. Christopher

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Feed for this Thread

Contact us