Tag Info

Hot answers tagged

12

For readers who didn't read all the comments, the slowdown is due to a lack of packing of tb, whereas RandomReal returns packed arrays when more than 250 elements are generated. The reason why packing tb fails is because some elements have different precision than others, and (I think?) ToPackedArray requires arrays to be of homogeneous type. To fix this, ...


8

I think you will find what you are looking for with MedianFilter mat = RandomReal[{0,1},{50,50}]; Animate[MatrixPlot[MedianFilter[mat,x]],{x,1,5,1}]


8

You can also use MapAt with Invisible or Style[#,White]&: f1 = MatrixForm[MapAt[Invisible, #, Position[#, Except[#2], {2}, Heads->False]]] &; (* or f1 = MatrixForm[MapAt[Style[#,White]&, #, Position[#, Except[#2], {2}, Heads->False]]] &; *) Example: m = RandomInteger[5, {5, 5}]; Row[Prepend[f1[m, #] & /@ {1, 2, 1 | 2}, ...


7

Oleksandr remarked on the memory required for a dense matrix. I shall attempt to explore SparseArray limitations. From this error message it appears that the dimensions of the array must be machine integers: SparseArray[{}, {2, 2}^70] SparseArray::adims: Array dimension specification {1180591620717411303424,1180591620717411303424} should be ...


6

Part 1: It has to do with packed arrays. Packed arrays occupy around 1/3 to 1/4 the size of unpacked arrays, giving the results you see. You can verify this as follows: << Developer` PackedArrayQ[Table[Table[i*j, {i, 1, 10}], {j, 1, 10}]] PackedArrayQ[Table[i*j, {i, 1, 10}, {j, 1, 10}]] PackedArrayQ[Table[Table[i*j, {i, 1, 100}], {j, 1, 100}]] ...


5

The eigenvectors are defined up to an arbitrary complex constant. In other words, if $v_i$ is an eigenvector then $c_iv_i$ is the same eigenvector. Usually eigenvectors is normalized so $|c_i|=1$. v9 = {{0.117433 + 0.4424 I, 0.552311 + 0.14137 I, 0.682245 + 0. I}, {-0.131023 - 0.625986 I, 0.730765 + 0. I, -0.163119 + 0.174212 I}, {0.010945 - ...


5

Here's a variation you might find useful: fn[a_?ArrayQ, x_] := Replace[a, p : Except[x] :> Style[p, White], {ArrayDepth@a}] // MatrixForm Now: fn[IdentityMatrix[3], 1] But because the non-selected elements are only styled white they appear when selected:


5

f[x_List, n_] := (x /. y_ /; y != n -> " ") // MatrixForm f[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 7] If you need to display only elements in list n, then use: g[x_List, n_List] := x /. y_ /; (MemberQ[Complement[Flatten[x], n], y]) -> " " // MatrixForm g[{{1,2,3},{4,5,6},{7,8,9}}, {3,7}] I'm sure there's a more elegant way to do this last ...


5

You can increase the performance by transposing your matrix and using Dot without explicit [[...]] << Developer` n = 2; m = 6; matrix = Table[Sin[1.0*i*j], {i, m}, {j, n}, {k, n}]; matrix3 = ToPackedArray@Transpose@matrix; vector = N@Transpose@{IdentityMatrix[n]}; vectorHC = ConjugateTranspose@vector; Flatten[vectorHC.matrix3.vector, {{1, 3, 2, 4, ...


5

As Nasser pointed out, the polar decomposition of a matrix is not pre-built into Mathematica, but it can be easily computed from the singular value decomposition. For reference, here is a method to do it: polarDecomposition[m_] := {#.#3\[ConjugateTranspose], #3.#2.#3\[ConjugateTranspose]} & @@ SingularValueDecomposition[m]; This tests that it actually ...


4

This is too long for a comment, but here's a comparison between Matematica 10.0.2 and MATLAB R2014b on OS X, using MATLink. There is no appreciable difference between their performance. Mathematica 10 performs significantly better than Mathematica 9 due to updated MKL libraries. Both MATLAB and Mathematica rely on the MKL for matrix multiplications. ...


4

You can pad missed elements and add a transposed matrix M = # + ConjugateTranspose@UpperTriangularize[#, 1] &@PadLeft@halfM; M // MatrixForm


4

The built-in functions may not always do exactly what you wish. You can make your own filter reasonably straightforwardly. For example, here is a definition that takes a threshold and changes the value to equal the mean in the region whenever the pixel is above that threshold. filt[x_] := Module[{}, meanX = Mean[Flatten[x]]; center = ...


4

You can use image-procession restoration with Inpaint. Some data with spikes: n = 100; A = GaussianFilter[#, 3 {3, 1}] &@RandomReal[1, {n, n}] + RandomChoice[{100, 1} -> {0, 0.3}, {n, n}]; ArrayPlot[A, PixelConstrained -> 2, PlotRange -> {0, 1}] After reconstruction: filter[A_, thr_] := ImageData@Inpaint[Image@A, Image@UnitStep[A - ...


3

This appears to be a precision issue. Increase the precision. S[i_, j_] = 8 Sqrt[1.7^(3 i + 3 j - 6)]/ (1.7^(i - 1) + 1.7^(j - 1))^3 // Rationalize // Simplify; H[i_, j_] = -8 Sqrt[1.7^(3 i + 3 j - 6)]/ (1.7^(i - 1) + 1.7^(j - 1))^3* (0.01*(1.7^(i - 1) + 1.7^(j - 1)) - 0.01^2*1.7^(i + j - 2)) // Rationalize // Simplify; ...


3

Following up on Martin B├╝ttner's comment this is perhaps best handled using Ordering. We can apply it to subarrays produced by Partition, though we will need to Flatten them first. I shall further use PartitionMap to improve memory performance. A starting array: SeedRandom[0] a = RandomInteger[9, {4, 6}]; a // MatrixForm $\left( ...


2

It seems to be a performance problem (bug?) in version 9.0.1. Version 7.0.1 is also slow, but perhaps is not capable of this calculation. However, version 8.0.4 can produce the correct result quickly, so there seems to be no good reason why 9.0.1 should take so long. I didn't try 9.0.0. The interesting thing, is that 9.0.1 has no problem with the purely ...


2

The following is a brute force approach to generate all matrices with the required structure. It is not recommended for use with "large" n. ClearAll[mF]; mF = With[{n = #, values = Join @@ Permutations /@ Subsets[Range[#2], {#3}], positions = Join @@ (Tuples /@ Subsets[{#, Reverse@#} & /@ Subsets[Range[#], {2}], {#3}])}, ...


2

A function using Developer`PartitionMap and ListConvolve to replace any element greater than or equal to a given threshold with the average of its four neighbors: ClearAll[filterF1]; filterF1 = Function[{mat, threshold}, Block[{xx, func, cm = {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}/4, rF = # /. {a_ + b_. xx :> (a/(1 - b))} &}, func = If[#[[2, ...


2

Here is a direct implementation using MapIndexed. map = {{1, 1} -> "NW", {1, 2} -> "N", {1, 3} -> "NE", {2, 1} -> "W", {2, 2} -> "0", {1, 3} -> "E",{3, 1} -> "SW", {3, 2} -> "S", {3, 3} -> "SE"}; mat = {{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}; mat0 = {{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}; mat = ArrayPad[mat0, 1, Infinity]; f[{ii_, ...


2

I would make use of Mathematica patterns in the assumptions: Assuming[{Dm[a_, a_][x_] == Dm[a_, a_][-x_]}, Simplify[Transpose[dmat[q]] == dmat[-q]]] (* True *) This tells Simplifythat a matrix element with equal indices (i.e. a diagonal element) is an even function. The problem with your code is that with Apply, you are replacing the equal sign with an ...


2

Perhaps you can use EdgeAdd and/or VertexAdd. For example: am1 = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}; ag1 = AdjacencyGraph[am1, VertexLabels -> Placed["Name", Center], VertexSize -> Medium] ag2 = EdgeAdd[ag1, 2 <-> 4] AdjacencyMatrix[ag1] // Normal (* {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}} *) AdjacencyMatrix[ag2] // Normal (* {{0, 1, 1, 0}, ...


1

If you want a purely matrix-based approach, you can try: m = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}; AdjacencyGraph[m, VertexLabels -> "Name"] ADD = {1, 2}; VADD = 0 Range[Length[m]]; For[i = 1, i <= Length[ADD], i++, VADD[[i]] = 1]; m = Append[m, VADD]; VADD = Append[VADD, 0]; m = Transpose[Append[Transpose[m], VADD]] AdjacencyGraph[m, VertexLabels -> ...


1

I propose a silly workaround, instead of a workable explanation: Get["tb.dat"]; xls = Range[-500, 500, 1000/(1000 - 1)] // N; test[tb_] := (Re[Conjugate[#].(xls*#)] & /@ tb;) // AbsoluteTiming; test[RandomComplex[{0., 1. + I}, Dimensions[tb]]] (* {0.002002, Null} *) test[tb] (* {0.040038, Null} *) test[tb + ConstantArray[0. + 0. I, Dimensions@tb]] (* ...


1

First load up the lists of matrices as you define them: n = 150; m = 50; matrix = Table[1.0*Sin[i*j*k], {i, 1, n}, {j, 1, n}, {k, 1, n}]; matrix2 = Table[Sin[RandomReal[{0, 1}, {n, n}]], {i, 1, n}]; Now let's check the sizes: ByteCount[matrix] (* 83174488 *) ByteCount[matrix2] (* 27024088 *) Very different! Now... <<Developer` matrix3 = ...


1

u = {{1, 1, -1, 1}} (* NOT u = {1 , 1, -1, 1}, which is a list, I think *) v = {{1}, {1}, {-1}, {1}} MatrixForm[u] MatrixForm[v] Also Transpose[u]



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