How to include OR in Array formula where ONLY SOME criteria need to be TRUE - Excel Part 2 - arrays

This is an extension of this question, which was answered by XOR LX.
In the original question, I wanted to count the number of rows in a matrix that included values that fulfilled at least one column-specific criteria. The columns referenced were sequential (e.g., A1:D4), but the situation requires indexing columns that are not sequential.
Take the data matrix:
A B C D E F G H
4 2 2 2 1 4 2 4
5 2 1 3 4 1
3 2 1 4 5 1
1 2 3 5 3 2 2 2
2 2 2 2 2 2 2 2
With the column-specific criteria:
Column A criteria: >2
Column B criteria: >2
Column C criteria: <2
Column D criteria: >4
Column E criteria: >2
Column F criteria: >3
Column G criteria: >2
Column H criteria: >3
In order to count the number of rows that have values for all columns, as well as meet at least column-specific criterion, we could use XOR LX's code:
=SUM(N(MMULT(IF(MMULT(N(A1:H5=""),TRANSPOSE(COLUMN(A1:H5)^0))=0,COUNTIF(OFFSET(A1,ROW(A1:H5)-MIN(ROW(A1:H5)),COLUMN(A1:H5)-MIN(COLUMN(A1:H5))),{">2",">2","<2",">4",">2",">3",">2",">3"}),0),TRANSPOSE(COLUMN(A1:H5)^0))>0))
...which might have some problems with handling blanks, so the alternative:
=ROWS(A1:D4)-COUNTIFS(A1:A5,"<=2",B1:B5,"<=2",C1:C5,">=2",D1:D5,"<=4",E1:E5,">=2",F1:F5,">=3",G1:G5,">=2",H1:H5,">=3")-COUNT(1/N(MMULT(N(A1:H5=""),TRANSPOSE(COLUMN(A1:H5)^0))>0))
...which as far as I have tested seems to handle blanks regardless of their frequency or location.
But what if only rows with data for certain should be counted, ignoring the other columns. Furthermore, what if only certain columns should be evaluated for their criteria?
Let's take the example above. Our goal is to count the number of rows with i) full data in certain columns and ii) entries in certain columns that meet at least one column-specific criterion.
The relevant columns that should have full data are A:C and F:H. The columns that should be evaluated for their criteria are A:C only. The solution is 2 since:
There are 5 rows in total, but only rows 1, 3, 4, and 5 qualify as they have full data in columns A:C and F:H. Row 2 has missing data in column A and also G, either of which should disqualify its inclusion. Notice that even though row 3 has missing data in columns D and E, it is still included because these columns are not important.
Rows 1 and 3 meet the specified criteria, since i) they have full data in columns A:C and F:H, and ii) at least one entry meets one of the criteria for columns A, B, and C; specifically, the value in row 1, column A (4 > 2), and row 3, column A (3 > 2) and column C (1 < 2). Notice that this is still only counted as one row meeting the criteria, even though the criteria for 2 columns is met.
Your thoughts would be most welcome. Please also show some love to XOR LX in the original post - I am sure you will find his solution helpful.

#PyjamaNinja has encouraged me to try and develop my 'lemma' to their earlier question.
The method is:
A Count rows with entries in all specified columns (A-C and F-H)
B Count rows with entries in all specified columns that do _not_ meet any of the criteria in the columns being evaluated (A-C).
Subtract B from A.
=COUNTIFS(A1:A5,"<>",B1:B5,"<>",C1:C5,"<>",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")-COUNTIFS(A1:A5,"<=2",B1:B5,"<=2",C1:C5,">=2",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")
With many thanks to #XOR LX.

Related

Compare if cells between two columns are exactly matched, partially matched and do not match

I have Table 1 below (codes represent responses). I want to compare Columns 2 and 3.
ID
Code A
Code B
100
A,B
B,A
101
B,D,C
D,C,E
102
E,A
B,C
If both columns have an equal number of responses and the responses are the same (regardless of position), then it is an exact match (assigned a value of 1).
If both columns do not have an equal number of responses but some responses are similar between the two columns, then it is a partial match (assigned a value of 2).
If both columns do not have any similar responses, then it is a no match (assigned a value of 3).
So the new table should look like:
ID
Code A
Code B
Match_type
100
A,B
B,A
1
101
B,D,C
D,C,E
2
102
E,A
B,C
3

Google Sheets - Increment cell value with ARRAYFORMULA and based on value from another field

I have a spreadsheet I'm creating and I have an ARRAYFORMULA for incrementing the number of a field based on another field. My formula looks like this (NOTE: my rows start on row 4 that is why there is a ROW(A4:A)-3):
=ARRAYFORMULA(IF(ROW(A4:A) = 4, 1, IF(B4:B = 1, ROW(A4:A)-3, (ROW(A4:A)-3)-(B4:B - 1))))
What I'm doing is creating groups (A) and then have a sequence counter (B) which is the number of rows within the group. I want the result to look like this where the A just picks up from where it left off (Note: B is manually entered):
A
B
1
1
2
1
3
1
3
2
3
3
4
1
5
1
However, my result is looking this this:
A
B
1
1
2
1
3
1
3
2
3
3
6
1
7
1
I know ROW gets me the row number but when I try and use INDEX formula:
=ARRAYFORMULA(IF(ROW(G4:G) = 4, 1, IF(H4:H = 1, INDEX(G4:G, ROW(G4:G)-1, 1) + 1, INDEX(G4:G, ROW(G4:G)-1, 1)))
to get the actual value of the prior cell I get a constant flashing like it's stuck in an infinite loop of some sort. I know I can probably just accomplish this without ARRAYFORMULA however, this spreadsheet will be shared and contains many other formulas that I just don't want people to have to cut and copy from the row above and get formulas all messed up. I'm dealing with non-technical people that need something to just work very simple.
Sample:
https://docs.google.com/spreadsheets/d/1FqndR4oTm_uaO7aUxYSgb3p0bZ6yBz-KnjUAD8kctd4/edit?usp=drivesdk
use:
=ARRAYFORMULA(COUNTIFS(A4:A, A4:A, ROW(A4:A), "<="&ROW(A4:A)))
reverse:
=ARRAYFORMULA(MMULT(TRANSPOSE((SEQUENCE(COUNTA(B4:B))<=
SEQUENCE(1, COUNTA(B4:B)))*IF(INDIRECT("B4:B"&COUNTA(B4:B)+ROW(B4)-1)=1, 1)),
SEQUENCE(COUNTA(B4:B))^0))

Intersection with two columns in each matrix

I would like to find the intersection of two columns in two matrix (see example below). So to find the position where A and B intersect -- in this case in position 3 and 5.
My solution so far, was to combine the two columns to one column and use intersect function on one column afterwards with a string. Is there a more elegant solution?
A = [1,1;1,3;1,4;2,1;2,5;3,1]
A =
1 1
1 3
1 4
2 1
2 5
3 1
B = [2,5;1,4]
B =
2 5
1 4
You can avoid combining the columns. When using intersect you can use the rows option.
A = [1,1;1,3;1,4;2,1;2,5;3,1]
B = [2,5;1,4]
[C,ia,ib] = intersect(B,A,'rows');
>>ib
3
5
Additionally, if you do not want the intersection result to be ordered you can use the stable option.
[C,ia,ib] = intersect(B,A,'rows','stable');
>>ib
5
3

Picking out elements of an array according to a specific rule

So I have a 100000-by-2 matrix in MATLAB. I want to extract only the elements in the second column which all have the same element for their corresponding row in the first column. E.g. if we have:
A = [1 2
3 4
2 6
1 5
4 1
1 3]
and then specify that we want all elements in the second column with 1 as their corresponding value in the 1st column. So I would want the above to become:
2
5
3
Does anyone know how to do this in MATLAB?
Matlab supports matrix operations, so you can do what you want without having to explicitly loop through the entire matrix like some other languages.
By using logical indexing, (more on that here https://www.mathworks.com/help/matlab/math/matrix-indexing.html?refresh=true)
you can extract the necessary elements from your matrix.
First you want to create a [0,1] array of rows in your matrix that satisfy your condition.
You can do that with
A(:,1) == 1
This will produce
1
0
0
1
0
1
Which means that the first, fourth and last rows satisfy the condition where the element in the first column of the matrix satisfies the condition that it equals one.
Now you can use this to produce the elements in the second column, by simply using it as an index of your original matrix.
A(A(:,1)==1,2)
to produce the required result.

Count based on column and row

I can't seem to find something quite like this problem...
I have an array table where each row contains a random assortment of numbers 1-N
On another sheet, I have a table with column and row headers numbered 1-N
I want to count how many rows in the array contain both the column and row headers for a given cell in the table. Since countifs only reference the current cell in the specified array, they don't seem to be working in this scenario.
Example array:
A B C D
1 3 5 7
1 2 3 4
2 3 4 5
2 4 6 8
...
Table results (symmetrical about the diagonal):
A B C D E F
. 1 2 3 4 5 ...
1 - 1 2 1 1
2 1 - 2 2 1
3 2 2 - 2 2
4 1 2 2 - 1
5 1 1 2 1 -
Would using nested countifs work?
I don't agree with your results corresponding to 4/2, which surely should be 3, not 2, but this formula, based on the array table being in Sheet1 A1:D4 and the results table being in Sheet2 A1:F6, placed in cell B2 of the latter, should work:
=IF($A2=B$1,"-",SUMPRODUCT(N(MMULT(N(COUNTIF(OFFSET(Sheet1!$A$1:$D$1,ROW(Sheet1!$A$1:$D$4)-MIN(ROW(Sheet1!$A$1:$D$4)),),CHOOSE({1,2},B$1,$A2))>0),{1;1})=2)))
Copy across and down as required.
Note: If your actual table is in fact much larger than that given, it will probably be worth adding a simple clause into the above to the effect that the results for approximately half of the cells are obtained from their symmetrical counterparts, rather than via calculation of this construction, thus saving resource.
Regards

Resources