Conversion from mathematica to matlab

  • Context: MATLAB 
  • Thread starter Thread starter ggeo1
  • Start date Start date
  • Tags Tags
    Mathematica Matlab
Click For Summary

Discussion Overview

The discussion revolves around converting a Mathematica code snippet that generates random points in 2D to MATLAB, with a focus on extending it to 3D. Participants explore various methods for generating random points, accessing specific elements, and manipulating arrays in MATLAB.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant shares their initial Mathematica code for generating random 2D points and seeks to replicate this in MATLAB for 3D points.
  • Another participant expresses confusion over accessing specific elements in a MATLAB array and notes that their results are not within the expected range.
  • Multiple participants discuss alternative methods for generating random points and accessing values, including the use of loops and functions.
  • One participant suggests a more concise way to generate the random points in a single line of code.
  • There is mention of using symbolic variables and array manipulation techniques to achieve desired outputs.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best methods for achieving their goals, as various approaches are proposed and explored without a definitive resolution.

Contextual Notes

Some participants express uncertainty about how to maintain the variability of generated points and how to properly access and manipulate arrays in MATLAB.

Who May Find This Useful

Individuals transitioning from Mathematica to MATLAB, particularly those interested in random number generation and array manipulation in programming.

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 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K