Message Boards Message Boards

0
|
6323 Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[?] Check entries and return output if condition is satisfied?

Posted 5 years ago

I would be grateful if someone could help me with this code.

I want to check whether all the entries in any column of the generated table is equal to one If the condition is satisfied in any column, the result shall be the column's heading. If the condition is not satisfied in all columns, the result shall be none.

For example, I am attaching a file that outputs a table. This table contains no columns with all values are equal to one, so I am expecting the result to be none.

Attachments:
POSTED BY: Nehal Elshaboury
7 Replies
Posted 5 years ago

Sure

onesCount // Select[Length@#[[2]] > 0 && #[[2, 2]] == rows - 1 &] // Map[First]
(* {1, 2, 62, 82, 96} *)

With some helper functions

countsOf[n_, matrix_] := 
 Table[{i, Flatten@Tally[Select[matrix[[All, i]], # == n &]]}, {i, 1, Last@Dimensions@matrix}]

withCount[n_] := 
 Select[Length@#[[2]] > 0 && #[[2, 2]] == n &] /* Map[First]

Index of columns in colmat that have 10 2's

countsOf[2, colmat] // withCount[10]
(* {97, 100, 110, 129, 139, 143, 149, 150, 153, 174, 205} *)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Check entries and return output if condition is satisfied

POSTED BY: Rohit Namjoshi

Yeah I got it now. I understood your comment. Thanks a million:) One last question please if we could re-shape the output to be only 1, 2, 62, 82, 96 without the number of ones in each column. couldn't thank you more. You really saved my day.

POSTED BY: Nehal Elshaboury
Posted 5 years ago

Yes. I believe I satisfied your requirements.

onesCount // Select[Length@#[[2]] > 0 && #[[2, 2]] == rows &]

Returns an empty list, as I showed above, because no columns satisfy the condition.

In the second example

onesCount // Select[Length@#[[2]] > 0 && #[[2, 2]] == rows - 1 &]
(* {{1, {1, 43}}, {2, {1, 43}}, {62, {1, 43}}, {82, {1, 43}}, {96, {1, 43}}} *) 

The first element of each sub-list is the index of the column that has one value not equal to one.

So columns 1, 2, 62, 82, 96 satisfy that condition, they all have 43 1's

e.g. column 96 has 43 1's and one 0

colmat[[All, 96]] // Tally
(* {{1, 43}, {0, 1}} *)
POSTED BY: Rohit Namjoshi

I don't want to know the position of ones in terms of (row, column)

POSTED BY: Nehal Elshaboury

I think you didn't understand me well. I am searching for the index of columns with all its values equal to one. In the attached example, actually there doesn't exist any column that contains all its elements equal to one.

POSTED BY: Nehal Elshaboury
Posted 5 years ago

Here is one way

{rows, columns} = Dimensions@colmat

(* Create list of column number and number of ones in the column *)
onesCount = 
 Table[{i, Flatten@Tally[Select[colmat[[All, i]], # == 1 &]]}, {i, 1, columns}]

(* Select the ones that have a count equal to the number of rows *)
onesCount // Select[Length@#[[2]] > 0 && #[[2, 2]] == rows &]

(* {} *}

(* Columns that have one value not equal to one *)
onesCount // Select[Length@#[[2]] > 0 && #[[2, 2]] == rows - 1 &]

(* {{1, {1, 43}}, {2, {1, 43}}, {62, {1, 43}}, {82, {1, 43}}, {96, {1, 43}}} *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract