# manipulation Table

I have a table 9x10000, and I need to manipulate in the following way: if one element of the fifth line is equal to zero, the corresponding columns has to be deleted, otherwise it has to be left.

I tried with an if and drop inside a for loop, but it does not work. Many thanks

• One way is to apply this composed function to your data: Composition[Transpose, DeleteCases[{_, _, _, _, 0, _, _, _, _}], Transpose] – Gustavo Delfino Apr 28 '20 at 12:36

## 2 Answers

ClearAll[f]
f = Transpose @ Delete[Position[#[], 0]] @ Transpose @ # &;


Example:

SeedRandom
table = RandomInteger[5, {9, 10}];

MatrixForm[MapAt[Style[#, Blue] &, #, {5, All}] /.
Style[0, _] :> Style[0, Red, Bold]] & @ table table2 = f @ table;
MatrixForm[table2] Also

ClearAll[f2]
f2 = #[[All, Complement[Range@Length@#, Flatten@Position[#, 0]] &@#[]]] &;

f2 @ table == f @ table

 True

• Thanks...works! – federica Apr 28 '20 at 15:20

Two variants using Position and streamlining a bit.

Kindly stealing kglr's matrix,

SeedRandom
table = RandomInteger[5, {9, 10}];


With Fold and Drop:

SetRow = 5;
RemCol = Flatten[Position[table[[SetRow]], 0]];
output = Fold[Drop[#1, None, {#2}] &, table, Reverse[RemCol]]


With MapThread

SetRow = 5;
RemCol = Position[table[[SetRow]], 0];
output = MapThread[Delete, {table, ConstantArray[RemCol, Length[table]]}]


I do not know about efficiency, and I believe many ways that use Apply or custom functions can be used.