6
$\begingroup$

Here is a list:

list = {{1, 2, 3, 2, 3}, {3, 1, 1, 2, 3}, {3, 2, 1, 3, 2}, {1, 2, 1, 1, 1}};

I wish to have all elements be zero except the first and the fourth in each row: desired:

list={{1, 0, 0, 2, 0}, {3, 0, 0, 2, 0}, {3, 0, 0, 3, 0}, {1, 0, 0, 1, 0}};

I have written:

Do[
  Do[
     If[Position[list, list[[i, j]]] != {i, 1} && 
      Position[list, list[[i, j]]] != {i, 4}, list[[i, j]] = 0]
     , {i, 1, 4}]
    ,{j, 1, 5}]

But it doesn't work correctly. Is there any way to reach this goal?

$\endgroup$
7
$\begingroup$
list.DiagonalMatrix[{1, 0, 0, 1, 0}]

or

Inner[Times, list, {1, 0, 0, 1, 0}, List] // MatrixForm

$$\left( \begin{array}{ccccc} 1 & 0 & 0 & 2 & 0 \\ 3 & 0 & 0 & 2 & 0 \\ 3 & 0 & 0 & 3 & 0 \\ 1 & 0 & 0 & 1 & 0 \\ \end{array} \right)$$

Edit

It may also be done as follows using Inner (see here):

Inner[Times, list, DiagonalMatrix[{1, 0, 0, 1, 0}]] // MatrixForm

 

With Dot and SparseArray:

list.SparseArray[{ {1, 1} -> 1, {4, 4} -> 1}, {5, 5}] // MatrixForm

In 'pseudocode':

newmat = oldmat.SparseArray[{ {<col-position-old>, <col-position-new>} -> 1,
          ...}, {< total-cols-old >, <total-cols-new>}]

 

For example, to create a new list with col-1-old -> col-4-new, col-4-old -> col-1-new, col-3-old -> col-2-new, and entries in all other columns equal to zero:

list1 // #.SparseArray[{ {1, 4} -> 1, {4, 1} -> 1, {3, 2} -> 
   1}, {Dimensions[#][[2]], 5}] & // MatrixForm

$$ \left( \begin{array}{ccccc} 2 & 3 & 0 & 1 & 0 \\ 2 & 1 & 0 & 3 & 0 \\ 3 & 1 & 0 & 3 & 0 \\ 1 & 1 & 0 & 1 & 0 \\ \end{array} \right)$$

$\endgroup$
6
$\begingroup$

To preserve the original list you could do

 res = list;
 res[[All, {2, 3, 5}]] = 0;
 res // MatrixForm

Or

MapAt[0&, list, {All, {2, 3, 5}}] // MatrixForm

Or (thanks @kglr)

ReplacePart[list, {{_, 2 | 3 | 5} -> 0}] // MatrixForm

All give

enter image description here

$\endgroup$
  • $\begingroup$ (+1) you can make the second one shorter using MapAt[0&,...]. $\endgroup$ – kglr Jul 5 '17 at 18:43
  • $\begingroup$ Thanks - just was doing it ;) $\endgroup$ – eldo Jul 5 '17 at 18:44
  • $\begingroup$ and for the last one: ReplacePart[list, {{_, 2 | 3 | 5} -> 0}]:) $\endgroup$ – kglr Jul 5 '17 at 18:46
  • $\begingroup$ Indeed - how nice!!! Never have seen this form before. Do you want to post it as another answer or should I update? $\endgroup$ – eldo Jul 5 '17 at 18:50
  • $\begingroup$ eldo, i suggest you should update. $\endgroup$ – kglr Jul 5 '17 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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