0
$\begingroup$

To best explain my problem, I will take the following example:

Consider the following matrix :

A = {{1, 2, 3, 4, ""}, {"", 2, 3, 4, ""}, {"", "", 3, 4, ""}, {"", "", "", 4, 5}}

I designed the following two algorithms :

ProjVt[x_, y_, pat_] :=
     Block[
                {X = x, Y = y, Grid11, Grid21, Resvf1},

                Grid11 = PosGrid[X, pat];

                Grid21 = ConstantArray["", 10000];

                Table[
                      Table[
                            Grid21[[ Grid11[[i, j]] ]] = X[[i, Grid11[[i, j]] [[1]]  ]],
                            {j, 1, Length[Grid11[[i]]], 1}
                           ],
                            {i, 1, Length[X], 1}
                    ];

                 Resvf1 =  Grid21[[1 ;; Length[Y]]]

            ];

and

ProjHt[x_, y_, pat_] :=
Block[
               {X = x, Y = y, Grid12, Grid22, Resvf2},

                Grid12 = PosGrid[X, pat];

                Grid22 = ConstantArray["", 10000];

                Table[
                       Grid22[[i]] = X[[ i , Grid12[[i, 1, 1]] ;; Grid12[[i, -1, 1]] ]],
                       {i, 1, Length[X], 1}
                     ];

                Resvf2 = DeleteCases[Grid22, ""]

            ];

The results provided by ProjVt and ProjHt in the case of matrix A are :

Resvf1 = {1,2,3,4,5}
Resvf2 = {{1, 2, 3, 4}, {2, 3, 4}, {3, 4}, {4, 5}}


Best known optimization :


ProjHt[X_]:= Map[Select[#, NumberQ] &, X]

ProjVt[X_]:=
Block[
      {step1, Res},
       step1 = Tranpose[x];
       Res = ProjHt[step1][[All,1]]
     ]

.

$\endgroup$
5
  • $\begingroup$ DeleteDuplicates@Cases[A, _?NumberQ, {2}] and as you have already written: Map[Select[#, NumberQ] &, A]? $\endgroup$
    – Öskå
    Commented May 14, 2014 at 17:30
  • $\begingroup$ Hey Doedalos. Note that I fixed Tr(X) to Tr[X]. Please try making code blocks using the shortcut control/command + k, rather than using ` (backtick) for large code blocks. $\endgroup$ Commented May 14, 2014 at 18:49
  • $\begingroup$ @Doedalos Well, replace A with X? :) And then ProjVtalt1[A] :) So you would need ProjVtalt1[mat_]:=DeleteDuplicates[Cases[mat,_?NumberQ,{2}]] $\endgroup$
    – Öskå
    Commented May 17, 2014 at 9:53
  • $\begingroup$ @Doedalos If you wish you can answer your own question :) That would be great :) $\endgroup$
    – Öskå
    Commented May 17, 2014 at 11:21
  • $\begingroup$ @Doedalos Please answer your own question with the informations provided in the comments. $\endgroup$
    – Öskå
    Commented May 17, 2014 at 16:16

2 Answers 2

2
$\begingroup$
A /. "" -> Sequence[]

{{1, 2, 3, 4}, {2, 3, 4}, {3, 4}, {4, 5}}

$\endgroup$
0
$\begingroup$

Best known optimization :

A = {{1, 2, 3, 4, ""}, {"", 2, 3, 4, ""}, {"", "", 3, 4, ""}, {"", "","", 4, 5}}

ProjHt[X_]:= Map[Select[#, NumberQ] &, X]

ProjVt[X_]:=
Block[
      {step1, Res},
       step1 = Transpose[x];
       Res = ProjHt[step1][[All,1]]
     ]

ProjHt[A]
{{1, 2, 3, 4}, {2, 3, 4}, {3, 4}, {4, 5}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.