Tag Info

Hot answers tagged

18

I think the built-in function ArrayComponents is what you need: vec = {1, 4, 4, 8, 7, 7, 4}; ArrayComponents[vec] (* {1,2,2,3,4,4,2} *) mat = {{1, 4}, {2, 7}, {7, 2}, {9, 4}}; ArrayComponents[mat] (* {{1,2},{3,4},{4,3},{5,2}} *) raggedarray = RandomSample /@ (CharacterRange["a", "z"][[#]] & /@ Range[RandomSample[Range[5]]]) (* ...


11

The old-school way to do this: index[a_] := Module[{i = 1, f}, f[x_] := f[x] = i++; f /@ a] index @ vec {1, 2, 2, 3, 4, 4, 2} A method using Assocation, introduced long after ArrayComponents. index2[a_List] := AssociationThread[#, Range@Length@#] ~Lookup~ a & @ DeleteDuplicates @ a Edit #2: extended to matrices using eldo's own method: ...


4

I'm surprised MMA doesn't have something like StringSplit for list, but maybe I haven't looked hard enough. I added the .. to delete multiple all-zero columns after seeing @Kuba's comment. Please upvote his comment instead of my answer since his was the more succinct. mat = {{0, 0, 0, 0, 0, 1}, {1, 0, 0, 1, 0, 1}, {0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, ...


3

In the spirit of Mathematica 10 I would have written it like this: Composition[ DeleteCases[{{0} ..}], Map[Transpose], SplitBy[#, Unitize@*Total] &, Transpose ]@mat {{{0, 0}, {1, 0}, {0, 1}, {0, 0}, {1, 1}, {0, 0}}, {{0}, {1}, {0}, {0}, {1}, {1}}, {{1}, {1}, {0}, {1}, {0}, {1}}} With rules I would write: Transpose /@ {Transpose[mat] ...


3

Basically, the same idea as seismatica's answer, but with different details. m = {{0, 0, 0, 0, 0, 1}, {1, 0, 0, 1, 0, 1}, {0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1}, {1, 1, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 1}}; split = Transpose /@ DeleteCases[ SplitBy[If[Plus @@ # > 0, #] & /@ Transpose[m], # === Null &], {Null}]; ...


3

you can also use ClusteringComponents function inex[m_] := ClusteringComponents[m, Length@m + 1]; vec = {1, 4, 4, 8, 7, 7, 4}; inex[vec] (*{1, 2, 2, 3, 4, 4, 2}*) mat = {{1, 4}, {2, 7}, {7, 2}, {9, 4}}; inex[mat] (*{{1, 2}, {3, 4}, {4, 3}, {5, 2}}*)


2

Just for fun, here's a way to do it without using the built-in function: Clear[firstP] firstP[l_List] := l /. MapIndexed[#1 -> First@#2 &, DeleteDuplicates[Flatten@l]] firstP /@ {vec, mat} (* {{1, 2, 2, 3, 4, 4, 2}, {{1, 2}, {3, 4}, {4, 3}, {5, 2}}} *)


1

Bill s already gave an answer as a comment. An explicit proof of the conjecture can be obtained by noting that $$(A^3)_{il}=A_{ij}A_{jk}A_{kl}=\sum_{j=1}^N\sum_{k=1}^N\text{Boole}[i\leq j]\text{Boole}[j\leq k]\text{Boole}[k\leq l]\\ =\sum_{j=1}^N\text{Boole}[i\leq j]\sum_{k=1}^N\text{Boole}[j\leq k]\text{Boole}[k\leq l]\\ =\sum_{j=1}^N\text{Boole}[i\leq ...



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