Hot answers tagged

9

MapThread[Dot, {{A, B, C}, {X, Y, Z}}]


7

extractBDM[mat_?MatrixQ] := Pick[mat, #, 1] & /@ Values[ComponentMeasurements[ Image[MorphologicalTransform[Unitize[mat], "BoundingBoxes"]], "Mask", CornerNeighbors -> False]] /. {} -> Nothing Explanation I just treat the values as an image pixel.So our purpose is finding the connected component.Supose your list mat is ...


6

yourlist = {a, b, c}; mat = ReplacePart[Outer[Times, #, #]&@Prepend[yourlist, 1], {1, 1} -> state] Change yourlist to the list of your interest. Or, taking @mikado 's idea of symbolically diagonalizing: matfunc[1, 1] = state; matfunc[1, a_Integer] := yourlist[[a - 1]]; matfunc[a_Integer, 1] := yourlist[[a - 1]]; matfunc[a_Integer, b_Integer] := ...


5

{a, b, c, x, y, z} = RandomReal[{0, 1}, {6, 2, 2}] bk = Dot @@@ Transpose@{{a, b, c}, {x, y, z}} jd = MapThread[Dot, {{a, b, c}, {x, y, z}}] bk == jd (*True*) One can also use Inner[Dot, {a, b, c}, {x, y, z}, List] but you need to wrap lists in Unevaluated: Inner[Dot, Unevaluated@{a, b, c}, Unevaluated@{x, y, z}, List] See here for explanation.


5

Here's something even more compact than my proposal in the comments: Standardize[RandomReal[1, {4, 4}], 0 &, Total] If you must have a left stochastic matrix where all the entries should be greater than a set value, you can do rejection sampling: keep generating a matrix as long as the smallest value is smaller than the cutoff: While[Min[sm = ...


4

I gave a presentation on one (efficient) way to do it at the 2009 tech conference. The essence is this function: matrixAssembly[ values_, pos_, dim_] := Block[{matrix, p}, System`SetSystemOptions[ "SparseArrayOptions" -> {"TreatRepeatedEntries" -> 1}]; matrix = SparseArray[ pos -> Flatten[ values], dim]; System`SetSystemOptions[ "...


4

Update-2: More compact version proposed by J. M. with the matrix normalized: steps = 3; (*order = 2^step*) h = Nest[Join[KroneckerProduct[#, {1, 1}], KroneckerProduct[IdentityMatrix[Length[#]], {1, -1}]] &, {{1}}, steps]; Orthogonalize[h] // MatrixForm Update-1: Creates un-normalized Haar matrix for $\text{order} = 2^n$. steps = 3; ...


3

With Times: avec = Array[a, 4]; m = Outer[Times, avec, avec] /. {a[1] -> 1}; m[[1, 1]] = s; MatrixForm[m] Eigenvalues[m] Here a[2] is your a, a[3] is your b, etc. Change the 4 to 900 if you wish. There are two nonzero eigenvalues.


3

Here is a neat way: Array[Function[n, {{n, 7 n - 2, 3, 2 n - 1}, {3, 4, n, 6 n - 1}, {9, 3, n, n - 3}, {n - 2, 1, 8, 7 n/3}}], 10, 1, Dot]


3

I think for this kind of matrix it is better to use some of the dedicated matrix formats, like, "MTX" (of Matrix Market) or "HarwellBoeing". Below are two examples using "MTX": one with a dense 6000x6000 matrix and one with a sparse matrix. Dense matrix Mathematica mat = RandomReal[{0, 1}, {6000, 6000}]; Export["/path/RandomMat.mtx", mat, "MTX"] Python ...


3

SetDirectory[NotebookDirectory[]] (mat = RandomReal[1, {5, 5}]) // MatrixForm Export["mat.txt", mat, "CSV"] Open python and type >python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 >>> f = open ( 'mat.txt' , 'r') >>> mat = [ map(float,line.split(',')) for line in f ] >>> print(mat) [[0....


3

I am not sure if this is what you want (it would be helpful if you can give an example in your question). What I am doing here is to get the block matrix at any given position. First I create a general matrix. a = RandomInteger[{1, 9}, {2, 2}]; b = RandomInteger[{1, 9}, {3, 4}]; c = RandomInteger[{1, 9}, {2, 2}]; m = SparseArray[{Band[{1, 1}] -> a, Band[...


3

Your operation does not work because in this case it is not a matricial multiplication but a composition. Try mM = {{D[#, x] &, D[#, y] &}, {D[#, y] &, D[#, z] &}} v = {f[x, y, z], g[x, y, z]} apply[a_, b_] := Inner[#1[#2] &, a, b] This answer come from the article http://www.mathematica-journal.com/issue/v8i4/tricks/contents/...


3

Specify Eigensystem[h, 2, Method -> "Direct"] for both cases. That sparse array may go to a different solver (iterative) than the dense method. You can find more options in the documentation of Eigensystem under the options section.


3

To verify that the rotations happen the way they're supposed to according to the documentation for EulerMatrix, you could use the following Manipulate: Clear[arrowAxes]; arrowAxes[arrowLength_: 1] := Map[{Apply[RGBColor, #], Arrow[Tube[{-#, #}]]} &, arrowLength IdentityMatrix[3]] Manipulate[ Graphics3D[{GeometricTransformation[arrowAxes[.7], ...


3

Use Part to subtract intensity and Transpose to align with the wavelength data: wavelength = Range[350, 750, (400/3647)]; withMagnet = Transpose[{wavelength, RandomReal[1, 3648]}]; withoutMagnet = Transpose[{wavelength, RandomReal[1, 3648]}]; (*The above code just simulates your imported data*) diff = Transpose[{withoutMagnet[[All, 1]], ...


2

Here's a quick-and-dirty method that does what (I think) you're after: makearr[{n_, m_}, p_] := Module[{base = PadLeft[ConstantArray[1, Round[n m p]], n m], cand}, While[(cand = ArrayReshape[RandomSample@base, {n, m}]; Max[Total[cand]] > n - 2 || Max[Total /@ cand] > m - 2)]; Position[cand, 1]]; Example usage: makearr[{5, 5}, .4] {{...


2

Like J.M. said, the use of ListConvolve can be a bit tricky, because by default it cuts overhangs and has no padding. From your description i guess you are more interested in something like this: MatrixPlot[ ListConvolve[GaussianMatrix[10], BoxMatrix[20] - DiamondMatrix[20], {1, -1}, {0, 0}] ] $\mspace{172mu}\Big\Updownarrow$ $\mspace{30mu}$ This ...


2

Use Simplify with Assuming: m = {{E^(I*β1 + I*β3) Cos[β2], E^(I β1 - I*β3) Sin[β2]}, {(-E^((-I) β1 + I*β3)) Sin[β2], E^((-I) β1 - I*β3)*Cos[β2]}}; MatrixForm[ Assuming[{β1, β2, β3} ∈ Reals, Simplify@ConjugateTranspose[m]]] $$\left( \begin{array}{cc} e^{i \text{$\beta $1}+i \text{$\beta $3}} \cos (\text{$\beta $2}) & e^{i \text{$\...


2

In line with suggestion from @happy fish, I would write Energy = 1/2 (d1^2/2 + (-(d1/Sqrt[2]) + d2/Sqrt[2])^2) k - d1 P - d2 P - (d1^2/(2 L^2) + d2^2/(2 L^2)) P; d = {d1, d2}; ca = CoefficientArrays[Energy, d]; The list ca contains the term you are seeking, and satisfies ca[[1]] + ca[[2]].d + d.ca[[3]].d == Energy // Simplify Note that ca[[3]] is not ...


2

I suppose you would like something like this? Select[A.B.Transpose[R].R.T, FreeQ[#, R] &] This mainly used the expression's tree like property. Due to Dot's Flat Property, the whole expression will actually looks like Dot[A,B,Transpose[R],R,T], thus this code can generate the result you want: A.B.T Note that I've change your C to T cause C is a ...


2

I'll give two solutions here: a not-that elegant solution which can apply to multiple sizes but all blocks MUST be Fully filled! (Just the limitations of Morphological methods) Function[{n}, Pick[mat, #, n] /. {} -> Nothing] /@ Range@Max@# &@ MorphologicalComponents[mat, Method -> "BoundingBox", CornerNeighbors -> False] This use ...


2

I think try to create a zero-matrix and add your matrix on it will be helpful, just like the following code shows. In[22]:= kglobal2 = Module[{sum = ConstantArray[0, {8, 8}]}, sum[[1 ;; 4, 1 ;; 4]] += k12; sum[[{1, 2, -2, -1}, {1, 2, -2, -1}]] += k14\[Transpose]; sum ] Out[22]= {{9.92, 4.08, -6.08, -1.52, 0, 0, -3.84, -2.56}, {4.08, 380450., -1....


2

Reference: Part ListLinePlot[{myMatrix[[All,1]],myMatrix[[All,2]], ...}] data = {{1, 5, 10}, {1, 5, 10}, {1, 5, 10}, {1, 5, 10}, {1, 5, 10}}; ListLinePlot[{data[[All, 1]], data[[All, 2]], data[[All, 3]]}]] Or for a compact, universal solution (Courtesy of J.M.) ListLinePlot[Transpose[myMatrix]]


1

The matrix desired is at most rank two and can be easily expressed as the product of a matrix and its transpose. I will number the elements for convenience and assume that state > 1 (otherwise we have imaginary elements, which would require further thought). n = 3; v1 = Prepend[Array[a, n], 1]; v2 = Prepend[Array[0 &, n], Sqrt[state - 1]]; M1 = ...


1

Will this a-bit-complex code fullfill your need? arrayinsert[mat_, x_, y_] := With[{pre = ArrayFlatten@List@Riffle[#, Unevaluated@ConstantArray[0, {Length@#[[1]], x}]] & /@ mat}, ArrayFlatten[List /@ Riffle[pre, Unevaluated@ConstantArray[0, {y, Length@pre[[1, 1]]}]]]] ArrayPad[k12, {0, 4}] + arrayinsert[Partition[k14, {2, 2}], 4, 4] The key of ...


1

change your last two lines to: cT[{_, a2_, a3_}] := c11[m, n, a2, a3, F3, b]; cT /@ centroidList (*{80.1567, -95.8522, 0., 0., -80.1567, 95.8522, 0., 0.}*) Is this what you want?


1

You can do j=10; mat = Table[{{n, 7 n - 2, 3, 2 n - 1}, {3, 4, n, 6 n - 1}, {9, 3, n, n - 3}, {n - 2, 1, 8, 7 n/3}}, {n,j}]; Dot @@ Table[mat[[i]], {i,j}]//N Where j is the order to which you want to calculate the product, this gives as answer: {{7.28971*10^13, 1.09566*10^14, 7.92596*10^13, 2.71886*10^14}, {8.76822*10^13, 1.31681*10^14, 9....


1

I think what you want is ExpToTrig: matrix = {{1, 1, -1, -1}, {I k, -I k, -q, q}, {E^(I k a), E^(-I k a), -E^(-q b + I g (a + b)), -E^(q b + I g (a + b))}, {I k E^(I k a), -I k E^(-I k a), -q E^(-q b + I g (a + b)), q E^(q b + I g (a + b))}}; FullSimplify[ExpToTrig[Det[matrix]]] 4 I E^(I (a + b) g) (2 k q (Cos[(a + b) g] - Cos[a k] ...


1

If we ignore random sampling, this is a simple question of Boolean satisfiability: Module[{m, n, greens, matrix, sol}, m = 8; n = 5; greens = 20; matrix = Array[c, {m, n}]; sol = matrix /. First@FindInstance[ And @@ (BooleanCountingFunction[Length@# - 2, Length@#] @@ # & /@ matrix) && And @@ (...



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