Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
negative preallocation?

Subject: negative preallocation?

From: kamekame

Date: 5 May, 2009 19:46:30

Message: 1 of 13

Hello everybody!!

I use this preallocation
B=zeros(10,10,10,3)

to draw a solenoid in a coordinate system.
But i want to put the solenoid in the center of this coordinate system. This means, that i have to use the negative numbers. But how can i preallocate them?

Subject: negative preallocation?

From: Jan Simon

Date: 5 May, 2009 20:10:18

Message: 2 of 13

Dear kamekame!

> I use this preallocation
> B=zeros(10,10,10,3)
>
> to draw a solenoid in a coordinate system.
> But i want to put the solenoid in the center of this coordinate system. This means, that i have to use the negative numbers. But how can i preallocate them?

Sorry, the indices of arrays are always positive in Matlab.
Do you want the **values** of the array elements to be negative?

Kind regards, Jan

Subject: negative preallocation?

From: Gustavo Morales

Date: 5 May, 2009 20:22:02

Message: 3 of 13

kamekame <nanosecond@web.de> wrote in message <28128119.60067.1241552824307.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hello everybody!!
>
> I use this preallocation
> B=zeros(10,10,10,3)
>
> to draw a solenoid in a coordinate system.
> But i want to put the solenoid in the center of this coordinate system. This means, that i have to use the negative numbers. But how can i preallocate them?

Hi...! (excuses for my English)

It would be interesting to me know why are you using a 4D array of zeros to draw a 3D surface like a solenoid...
... but well... I think you don't need to preallocate negative or positive numbers. You use 'preallocation' to reserve memory for an array of doubles (B in this case) that would be modified inside a loop, so you can reduce the write time to that array.
It doesn't care if then you assign negatives or positives values to its elements: negatives or positives they are both of type 'double'. It sould take the same time to write in memory negatives or positives doubles.

Subject: negative preallocation?

From: kamekame

Date: 5 May, 2009 20:42:16

Message: 4 of 13

Hello Jan!

There is no problem if there are negative values. But my problem is the coodinate system. The middle of the solenoid should be at (0,0,0) and not at for example (5,5,5). How can I solve this problem. I hope you understand my problem.

Thanks a lot!

kamekame

Subject: negative preallocation?

From: kamekame

Date: 5 May, 2009 20:56:50

Message: 5 of 13

If i dont use preallocation i get this:

??? Undefined function or method 'B' for input arguments of type 'double'.
Error in ==> projekt008_SPULE at 67
B(x,y,z,1) = B(x,y,z,1) + dB(1);

What should I do?

I need 4-D because I want the x-, y- and z-component of dB. dB is the infinitesimal magnetic field.

Subject: negative preallocation?

From: TideMan

Date: 5 May, 2009 21:25:25

Message: 6 of 13

On May 6, 8:56 am, kamekame <nanosec...@web.de> wrote:
> If i dont use preallocation i get this:
>
> ??? Undefined function or method 'B' for input arguments of type 'double'=
.
> Error in ==> projekt008_SPULE at 67
> B(x,y,z,1) = B(x,y,z,1) + dB(1);
>
> What should I do?
>
> I need 4-D because I want the x-, y- and z-component of dB. dB is the inf=
initesimal magnetic field.

You don't understand about addresses in arrays.
They are integers representing the position relative to the start, not
the numbers themselves.
So, for your array B the first column is addressed like this: B
(1,1,1,1), B(2,1,1,1), B(3,1,1,1) .. up to B(10,1,1,1).
Now, what those correspond to in terms of your x is entirely up to
you.
You say x goes from -5 to 5.
This means you need 11 rows (not 10).
B(1,1,1,1) is for x=-5
B(2,1,1,1) is for x=-4
B(3,1,1,1) is for x=-3
B(4,1,1,1) is for x=-2
B(5,1,1,1) is for x=-1
B(6,1,1,1) is for x=0
B(7,1,1,1) is for x=1
B(8,1,1,1) is for x=2
B(9,1,1,1) is for x=3
B(10,1,1,1) is for x=4
B(11,1,1,1) is for x=5

Subject: negative preallocation?

From: Gustavo Morales

Date: 5 May, 2009 21:34:01

Message: 7 of 13

kamekame <nanosecond@web.de> wrote in message <8643279.60466.1241557083131.JavaMail.jakarta@nitrogen.mathforum.org>...
> If i dont use preallocation i get this:
>
> ??? Undefined function or method 'B' for input arguments of type 'double'.
> Error in ==> projekt008_SPULE at 67
> B(x,y,z,1) = B(x,y,z,1) + dB(1);
>
> What should I do?
>
> I need 4-D because I want the x-, y- and z-component of dB. dB is the infinitesimal magnetic field.

Kamekame:
Then you don't want to plot the solenoid. You want to plot the magnetic field intensity instead, Don't you?.

Let's do the following: Write the solenoid equation here (in cartesian coordinates, preferable)

Subject: negative preallocation?

From: someone

Date: 5 May, 2009 21:35:01

Message: 8 of 13

kamekame <nanosecond@web.de> wrote in message <8643279.60466.1241557083131.JavaMail.jakarta@nitrogen.mathforum.org>...
> If i dont use preallocation i get this:
>
> ??? Undefined function or method 'B' for input arguments of type 'double'.
> Error in ==> projekt008_SPULE at 67
> B(x,y,z,1) = B(x,y,z,1) + dB(1);
>
> What should I do?
>
> I need 4-D because I want the x-, y- and z-component of dB. dB is the infinitesimal magnetic field.

% I think you need to think through your problem a little more.
% Its still not clear you need a 4-D array. Why won't this work?

B(x,y,z) = B(x,y,z) + dB(1);

% As stated before, you can't have negative indicies.
% All that means is you need to translate them in your code.

% For example if x, y, and z are the "zero centerd" coordinates
%(going from -5 to +5) for example, then simply assign
i = x+6;
j = y+6;
k = z+6;

% Then use B(i,j,k) instead of B(x,y,z) where
B = zeros(11,11,11);
% Note: x, y, & z must be integers
% and the "zero center" is at B(6,6,6)

% But, again, think this through before you code it!
% You may find its easier than you think.

Subject: negative preallocation?

From: kamekame

Date: 5 May, 2009 21:38:19

Message: 9 of 13

I understand adressing in arrays. But i thougt there would be a trick to go into the negative numbers.

Thanks a lot!

Subject: negative preallocation?

From: Gustavo Morales

Date: 5 May, 2009 21:52:01

Message: 10 of 13

kamekame <nanosecond@web.de> wrote in message <28128119.60067.1241552824307.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hello everybody!!
>
> I use this preallocation
> B=zeros(10,10,10,3)
>
> to draw a solenoid in a coordinate system.
> But i want to put the solenoid in the center of this coordinate system. This means, that i have to use the negative numbers. But how can i preallocate them?

You want to make a surface plot, don't you?
If you give an equation for a surface (implicit, explicit or parametric), in cartesian coordinates, I'll tell you what to do

Subject: negative preallocation?

From: kamekame

Date: 5 May, 2009 22:18:41

Message: 11 of 13

but i think there is no function. i use the law of biot-savart to calculate the magnetic field.

Subject: negative preallocation?

From: kamekame

Date: 5 May, 2009 22:16:28

Message: 12 of 13

This version is good, and i had the same idea. But there is a new problem with streamslice.

I need some time to think about it. And then i will post the code.

Thanks!

Subject: negative preallocation?

From: Steven Lord

Date: 6 May, 2009 03:58:56

Message: 13 of 13


"kamekame" <nanosecond@web.de> wrote in message
news:19586467.60701.1241559537660.JavaMail.jakarta@nitrogen.mathforum.org...
>I understand adressing in arrays. But i thougt there would be a trick to go
>into the negative numbers.

No, there isn't. I think what you want to do is to create multiple
matrices -- two or three for coordinates and one for your data. For
example:

[xm, ym, zm] = peaks;

The value of the PEAKS function at x = xm(4, 5) and y = ym(4, 5) is z =
zm(4, 5).

You should also read this section of the documentation, which talks in more
depth about creating 3-D graphs:

http://www.mathworks.com/access/helpdesk/help/techdoc/visualize/f0-5799.html

--
Steve Lord
slord@mathworks.com

Tags for this Thread

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.

Contact us