# Conversion from mathematica to matlab

• MATLAB
• ggeo1
In summary, the conversation discusses a problem in converting code from Mathematica to MATLAB in 3 dimensions, specifically with the output being in one dimension instead of three. The person seeks help in writing the code and finding a solution to the problem. The conversation also covers the use of function handles in MATLAB.
ggeo1
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.

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

I understand the importance of being able to convert code between different programming languages. In this case, the code is converting from Mathematica to MATLAB, specifically in 3 dimensions. The original code in Mathematica generates a list of 10,000 random points in a 2-dimensional space, while the code in MATLAB aims to do the same in a 3-dimensional space.

To achieve this, I would suggest using the "rand" function in MATLAB, which generates a random number between 0 and 1. By multiplying this with 2 and subtracting 1, we can get a random number between -1 and 1, similar to the RandomReal function in Mathematica.

The code in MATLAB can be written as follows:

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

This will generate a 3x10000 matrix of random points, and experiment(10) will give the 10th point in the matrix, which will be a 3-dimensional point. I hope this helps in converting your code from Mathematica to MATLAB. Good luck!

## What is the difference between Mathematica and Matlab?

Mathematica and Matlab are both popular software programs used for mathematical and scientific computations. While both have similar capabilities, they differ in terms of programming language and user interface. Mathematica uses its own proprietary language, while Matlab uses a more traditional programming language. Matlab also has a more user-friendly interface and is better suited for numerical calculations, while Mathematica is better for symbolic calculations.

## Why would someone want to convert from Mathematica to Matlab?

There could be a variety of reasons for wanting to convert from Mathematica to Matlab. Some possible reasons include wanting to take advantage of Matlab's specific features or functions, needing to collaborate with others who use Matlab, or simply preferring Matlab's programming language and user interface.

## Is it difficult to convert code from Mathematica to Matlab?

The difficulty of converting code from Mathematica to Matlab depends on the complexity of the code and the familiarity of the programmer with both languages. While there are some differences in syntax and functions, many basic calculations and operations can be easily converted between the two programs.

## Are there any tools or resources available to assist with the conversion process?

Yes, there are several tools and resources available online to assist with converting code from Mathematica to Matlab. These include online converters, tutorials, and forums where users can ask for help or advice.

## Are there any limitations when converting from Mathematica to Matlab?

While many basic calculations and operations can be easily converted, there may be limitations when converting more complex code or functions. It is important to thoroughly test and debug the converted code to ensure it is functioning properly in Matlab.

• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
240
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
203
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K