MATLAB Conversion from mathematica to matlab

AI Thread Summary
The discussion revolves around converting Mathematica code for generating random 3D points into MATLAB. The user initially struggles with generating a 3D array in MATLAB and receives guidance on using the command "experiment=2*rand(npoints,3)-1" to create the desired 3D points efficiently. They also inquire about obtaining square roots in a specific format and receive suggestions for using symbolic variables and cell arrays. The conversation highlights the user's transition from Mathematica to MATLAB, emphasizing the need for concise solutions and function usage for generating random values. Ultimately, the user finds a solution for both generating random points and managing variable outputs in MATLAB.
ggeo1
Messages
61
Reaction score
0
Hello,
i have the following problem in mathematica and i want to convert it in MATLAB ,but in 3 dimensions.

Code:
npoints = 10000;
vectorpoint := {RandomReal[{-1, 1}], RandomReal[{-1, 1}]}
experiment = Table[vectorpoint, {npoints}];
experiment[[10]]

the result is for example {-0.921607, -0.389386}

Now, in MATLAB (for 3d)

Code:
npoints = 10000;
v1=2*rand-1;
v2=2*rand-1;
v3=2*rand-1;
vectorpoint=[v1,v2,v3];
experiment=(vectorpoint:npoints);
experiment(10)

The problem is that the result is for example
9.9238 , a one dimension ,but i want 3d.

I can't figure how i must write it.
 
Physics news on Phys.org
Hello ,

I tried : experiment([10 10 10]) but it gives me : 8.6808 8.6808 8.6808
But it is supposed to be in the range (-1,1).

Any help?

EDIT----------------------------
Also,how can i do sth like this?

[x y z]=sqrt(1:3)

in order to be x=1 , y=1.4142 , z=1.7321


EDIT------------------------------
For the previous , i did this:

Code:
syms x y z
vec=[x y z]
new=sqrt(1:length(vec))

new=1.0000    1.4142    1.7321   (is there another way to do all this?)

EDIT:::

I also did this :

Code:
vector=[v1,v2,v3]
new=num2cell(vector)
[x,y,z]=new{:}

And finally,

if i name for example ,the previous statement , vector=[x y z]
How can i do this work? ezplot3(vector,'r')

I am new to MATLAB ( i have done some mathematica though)

Thanks!
 
Last edited:
Hello,
I updated my code and now i am at this :

Code:
npoints=10000;
vectorpoint=2*rand(1,3)-1
for i=1:npoints
   2*rand(1,3)-1
    end

1) Is there another way to write this :
for i=1:npoints
2*rand(1,3)-1
end
in an one line?

2) I wrote in the loop 2*rand(1,3)-1 because if i write vectorpoint it will keep only one value of it.Is there a way to write vectorpoint and give me different values every time i call it?

If anyone can help...
 
Ok, here is the solution

1) Just this : experiment=2*rand(npoints,3)-1

2) As for the vectorpoint,probably only with function
 

Similar threads

Replies
5
Views
3K
Replies
1
Views
2K
Replies
10
Views
3K
Replies
4
Views
4K
Replies
1
Views
2K
Replies
7
Views
2K
Replies
2
Views
2K
Replies
4
Views
1K
Back
Top