Tag Info

Hot answers tagged

43

2014-04-12 NOTICE: The recently released MATLAB R2014a contains a bug which partially breaks MATLink (it's not possible to MGet logical type variables, which in turn may have further consequences). If you depend on MATLink, please consider keeping MATLAB R2013b until the problem is sorted out. Due to the nature of the problem there is no quick workaround ...


20

There is the ToMatlab package that will convert Mathematica expressions to MATLAB equivalents. For example: <<ToMatlab` Expand[(x + Log@y)^5] // ToMatlab (* x.^5+5.*x.^4.*log(y)+10.*x.^3.*log(y).^2+10.*x.^2.*log(y).^3+5.* ... x.*log(y).^4+log(y).^5; *) It even conveniently breaks it using ... and can also convert matrices: RandomInteger[5, ...


19

There was an update for Array, not done to the end. The method below does not work for earlier versions even though that Array is New in 1 | Last modified in 4 Moreover WRI forgot to update docs for error messages: Array::plen - the first example gives no error in V9. V9 Array[# &, n, {start, stop}] Array[# &, 10, {-1, 1}] {-1, ...


17

Based on the MATLAB documentation, it would appear that this is accomplished by simple zero-filling. As such, you can obtain the same result in Mathematica using Fourier[PadRight[list, n, 0.], FourierParameters -> {1, -1}] where list is your signal and n is the desired length. For a multidimensional FFT, replace n with {n1, n2, ...}, where n1, n2, ...


16

Here's a somewhat simpler way: logspace[a_, b_, n_] := 10.0^Range[a, b, (b - a)/(n - 1)] This gives a sequence starting at 10^a and ending at 10^b, with n points logarithmically spaced, as does MATLAB's logspace() function.


15

According to the Mathematica documentation on this page: Here is how to define a 5(4) pair of Dormand and Prince coefficients [DP80]. This is currently the method used by ode45 in MATLAB. DOPRIamat = { {1/5}, {3/40, 9/40}, {44/45, -56/15, 32/9}, {19372/6561, -25360/2187, 64448/6561, -212/729}, {9017/3168, -355/33, 46732/5247, 49/176, ...


12

My take: toward[p1_, p2_, v_: .05] := p1 + v Normalize[p2 - p1]; {n, r} = {4, 3}; DynamicModule[{pts, history}, pts = r {Cos[#], Sin[#]} & /@ Range[2 Pi/n, 2 Pi, 2 Pi/n]; history = {pts}; Print[Dynamic[ListPlot[Transpose@history, AspectRatio -> Automatic, Joined -> True, PlotStyle -> Directive[Thick, CapForm["Round"]], ...


12

Blend gives smooth transition between a list of colors: cf[v_] := Blend[{Black, Red, Yellow, White}, v] DensityPlot[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, ColorFunction -> cf, PlotPoints -> 300] For a clear distinction between some set of contours you can use ContourPlot: ContourPlot[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, ColorFunction -> ...


11

(Update, added more points, and more timings) Using MATLAB's help standard example for meshgrid: Mathematica implementation meshgrid[x_List, y_List]:={ConstantArray[x,Length[x]],Transpose@ConstantArray[y,Length[y]]} {xx, yy} = meshgrid[Range[-2, 2, .1], Range[-4, 4, .2]]; c = xx*Exp[-xx^2 - yy^2]; pts = Flatten[{xx, yy, c}, {2, 3}]; ListPlot3D[pts, ...


10

Here I show the basic way to call MATLAB using NETLink under Windows via the MATLAB COM interface. This answer is Community Wiki, feel free to extend it to others platforms and/or improve it! In[1]:= Needs["NETLink`"] matlab = CreateCOMObject["matlab.application"] Out[2]= «NETObject[COMInterface[MLApp.DIMLApp]]» Now one can invoke MATLAB functions: ...


10

You could try something like the following. bounded[Indeterminate] := $MaxMachineNumber; bounded[x_?NumericQ] := x This gives a very large number instead of Indeterminate so NMinimize keeps searching. NMinimize[bounded[J[{θ0, θ1, θ2}, X1, y1]], {θ0, θ1, θ2}] (* {0.203498, {θ0 -> -25.1613, θ1 -> 0.206231, θ2 -> 0.201471}} *) Incidentally, ...


10

If you make all the component parts matrices, you can use ArrayFlatten c = {{{{1}}, {{2, 3, 4}}}, {{{5}, {9}}, {{6, 7, 8}, {10, 11, 12}}}}; c // MatrixForm ArrayFlatten[c] // MatrixForm


8

I don't belive there is a build in function for this, however you can easily do it using Range fSpace[min_, max_, steps_, f_: Log] := InverseFunction[f] /@ Range[f@min, f@max, (f@max - f@min)/(steps - 1)] Inverse functions are being used so it'll give warnings in cases where you should be cautius, however it works for Log and other invertible ...


8

Alternatively: newcolor = RGBColor /@ {{0, 0, 0}, {0.2`, 0, 0}, {0.4`, 0, 0}, {0.6`, 0, 0}, {0.8`, 0, 0}, {1, 0, 0}, {1, 0.2`, 0}, {1, 0.4`, 0}, {1, 0.6`, 0}, {1, 0.8`, 0}, {1, 1, 0}, {1, 1, 0.2`}, {1, 1, 0.4`}, {1, 1, 0.6`}, {1, 1, 0.8`}, {1, 1, 1}}; dat = With[{y = Range[-2, 2, .04]}, Table[Exp[-x^2 - y^2], {x, y}]]; ListDensityPlot[dat, ...


8

Bear in mind that I'm not completely sure what you are calculating, here is my 5 cents towards what can help you get a better performance. I haven't benched marked the MATLAB code on my system, but the changes implemented in Mathematica lead to a runtime decrease from 78.036 s down to to 0.171 s. The slowing factors where mainly that you handle a lot of ...


7

Your question is a bit confusing to me because imshow and DensityPlot do different things. imshow will take a matrix of values and show it as an image. The Mathematica equivalent is Image. DensityPlot will take a two-argument function and plot it in 2D. Here's a direct comparison (using MATLink to pass the data to MATLAB, for convenience): If you need ...


7

I'll leave this up on GitHub, but I won't maintain the port. I recommend using MATLink instead. There's a package on the Wolfram Library Archive called mEngine that allows calling MATLAB from Mathematica. What it can do is: execute arbitrary MATLAB commands and retrieve their output as a string transfer array variables between Mathematica and MATLAB ...


7

There are already good explanations about how the restructuring of the nested arrays given as input to cell2mat can be done in Mathematica. I couldn't resist to mention the following, though: The main purpose of the matlab function cell2mat is to convert from so called cell arrays to "normal" matlab matrices, where cell arrays correspond roughly to "normal" ...


6

c = {{{1}, {2, 3, 4}}, {{{5}, {9}}, {{6, 7, 8}, {10, 11, 12}}}}; This works for this example, perhaps you can tweak it to your needs. Switch[Depth[#], 3, Join @@ #, 4, Sequence @@ Join @@@ Transpose[#]] & /@ c


6

If you want to translate Matlab code into Mathematica, my advice is - don't! As programming languages, the two are very different and an idiom that works well in one is unlikely to work well in the other. A fundamental theorem theorem in discrete dynamics states that if there's an attractive orbit, then it must attract at least one critical point. Thus, ...


5

Sorry I can't make sense of your Matlab code - too many nested Ifs. I don't know whether I've adapted belisarius' answer correctly: point = RandomInteger[100, {3}]; triangle = RandomInteger[100, {3, 3}] ; lines = Subsets[triangle, {2}]; nline[{start_, end_}, pt_] := Module[{param = ((pt - start).(end - start))/Norm[end - start]^2}, N@{pt, start + ...


5

There is an alternative solution to implement AMD into Mathematica, by using the MinCut function from the GraphUtilities Package. This function is not just working on Graphs but is also usable for Matrices. The algorithm is reordering the Matrix into blocks and effectively reducing the off diagonal elements. It can be involved rather easily. If one starts ...


5

I have found the following page useful for understanding what the equivalent commands are: http://meng6.net/pages/matlab_mathematica_equivalent_commands/ If you're looking for a package that actually translates between mathematica and matlab, this previous question/answer is a good place to start: Is it possible to export the equations from Mathematica to ...


5

More for my own personal edification and possible enlightenment from more experienced users, here are two other possibilities. First, Use a sparse array with a size much larger than expected. a = Range@3; b = SparseArray[a, 10^7]; It results in a bit of overhead ByteCount /@ {a, b} (* {128, 720} *) Which decreases with increasing (initial) array size ...


5

why not simply: x2[n_, a_] := PadRight[x, n, 0] + (SparseArray[n -> a] // Normal) x2[10,5] (*{1, 2, 3, 4, 0, 0, 0, 0, 0, 5}*)


4

Concise Another way to define meshgrid() in Mathematica is: meshgrid[xgrid_List, ygrid_List] := Transpose[Outer[List, xgrid, ygrid], {3, 2, 1}] It's definitely not winning the speed competition (about 10x slower than rm -rf's solution), but speaks more for Mathematica's language, which allows neat and concise definitions like this. Speed Regarding ...


4

meshgrid()'s functionality is easily handled by ConstantArray. For the example you provided (i.e. meshgrid called with a single vector), {a, b} = {#, Transpose@#}&@ConstantArray[N@Range@3000, {3000}]; // AbsoluteTiming (* {0.068030, Null} *) You can easily extend it to handle the two argument case by calling ConstantArray for each list (x and y) and ...


4

Might try without approximate values. To do this, instead use numericValues = {Vphrms -> 230, Vlinerms -> Sqrt[3] 230, \[Omega] -> 2 \[Pi] 50, L1 -> 23*10^-4, L2 -> 93*10^-5, Cf -> 10*10^-6, Co -> 600*10^-6, rd -> 3/10, r1 -> 1/5, r2 -> 1/5, Dd -> 623324/1000000, Dq -> 246624/10000000, Vdc -> 650, I1d ...


4

You could use table... unless I am missing something really basic. Speed, maybe? Edited to consider the special case as suggested by Kuba. linspace[start_, stop_, n_:100] := Table[x, {x, start, stop, (stop - start)/(n - 1)}] linspace[start_, stop_, 1] := Mean[{start,stop}]


4

The problem is that the symbolic vector {x,y,z} is passed to the function before the values for x, y and z are substituted. Symbols cannot be transferred to MATLAB, so you get an error: In[1]:= Needs["MATLink`"] In[2]:= OpenMATLAB[] In[3]:= norm = MFunction["norm"] Out[3]= MFunction["norm"] In[4]:= norm[{x, y, z}] During evaluation of In[4]:= ...



Only top voted, non community-wiki answers of a minimum length are eligible