4
$\begingroup$

Given a 3x3 matrix, check each of the columns starting from the first one, and if a column contains 1 or -1 the output is x, otherwise the output is y. Then list the sequence of these x,y as a vector. For example, let A={{2,1,1},{0,1,0},{0,0,1}}, then f is the vector where f(1)=y, f(2)=x, f(3)=x, according to the given matrix

I tried the following but it needs to be revised:

 For[k = 1, k <= 3, k++, For[z = 1, z <= 3, z++,  If[A[[z, k]] == 1 || A[[z, k]] == -1,  f[[k]] = y,  f[[k]] = x;  ];  ];  ]; f[[1]], f[[2]], f[[3]]

I also tried this one but in this version if a column has 1 then output is x otherwise the output is y

For[k = 1, k <= 3, k++, If [A[[1, k]] == 1  || A[[2, k]] == 1 || A[[3, k]] == 1, f[[k]] = y,  f[[k]] = x; ]; ];
$\endgroup$
3
  • 1
    $\begingroup$ What have your tried? $\endgroup$
    – A.G.
    Commented Jun 7, 2021 at 13:35
  • 1
    $\begingroup$ Your result is not consistent with the operation described. $\endgroup$
    – Bob Hanlon
    Commented Jun 7, 2021 at 13:36
  • $\begingroup$ Thanks I just wrote my try, but It not works. Thanks @Bob Hanlon, I revised it. It is consistent now. $\endgroup$
    – gunes
    Commented Jun 7, 2021 at 14:00

6 Answers 6

3
$\begingroup$

Transpose and then Map

{{2, 1, 1}, {0, 1, 0}, {0, 0, 1}} // Transpose // 
 Map[If[MemberQ[#, 1] || MemberQ[#, -1], x, y] &]

Or

{{2, 1, 1}, {0, 1, 0}, {0, 0, 1}} // Transpose // 
 Map[If[ContainsAny[#, {1, -1}], x, y] &]
{y, x, x}
$\endgroup$
5
  • $\begingroup$ Thanks for the code , I understand this one is considering if a column has 1 then x and otherwise y, For adapting if a column has 1 or -1 then x, do we need to only change MemberQ[#, 1] as MemberQ[#, 1,-1] ? $\endgroup$
    – gunes
    Commented Jun 7, 2021 at 14:47
  • 1
    $\begingroup$ @gunes fixed code. $\endgroup$ Commented Jun 7, 2021 at 15:48
  • $\begingroup$ thanks. I was wondering How we can adapt your code to for loop case. I suppose we should start as f = {}; For[i = 1, i <= 3, i++, after that how can we go on? $\endgroup$
    – gunes
    Commented Jun 7, 2021 at 15:57
  • 1
    $\begingroup$ @gunes It's not recommended to use For in mma (Though mma has this function) $\endgroup$ Commented Jun 7, 2021 at 17:17
  • 1
    $\begingroup$ @wuyudi @gunes ...//Map[If[MemberQ[#, 1 | -1], x, y] &] is also fine. $\endgroup$ Commented Jul 21, 2021 at 23:49
2
$\begingroup$
list = {{2, 1, 1}, {0, 1, 0}, {0, 0, 1}};

ArrayReduce[FirstCase[#, 1 | -1 :> x, y] &, list, 1]

{y, x, x}

$\endgroup$
2
$\begingroup$
Map[AnyTrue[Abs@# == 1 &], Transpose@list] /. {True -> x, False -> y}

{y, x, x}

$\endgroup$
2
$\begingroup$
list = {{2, 1, 1}, {0, 1, 0}, {0, 0, 1}};

Using ReplacePart:

ReplacePart[list, i_ :> If[! FreeQ[Transpose[list][[i]], 1 | -1], x, y]]

(*{y, x, x}*)
$\endgroup$
2
  • 1
    $\begingroup$ Please try with {{1, 1, 1}, {0, 1, 0}, {0, 0, 1}} ? OP requires looking through columns. $\endgroup$
    – Syed
    Commented Mar 8 at 5:23
  • 1
    $\begingroup$ @Syed Thanks for the feedback! See the update, please. $\endgroup$ Commented Mar 8 at 17:30
2
$\begingroup$
list = {{2, 1, 1}, {0, 1, 0}, {0, 0, 1}};

Using MapThread and ContainsNone

MapThread[If[ContainsNone[{##}, {1, -1}], y, x] &, list]

{y, x, x}

$\endgroup$
1
$\begingroup$
list = {{2, 1, 1}, {0, 1, 0}, {0, 0, 1}};

{y, x}[[1 + Boole @* MemberQ[1 | -1] /@ Transpose @ list]]

{y, x, x}

$\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.