How to grab the last non empty row? - if-statement

So here's the scenario. In this case i will start at row 2 as starting point. in Column B, i want to record row of the last non empty A column. For example : the first data in B column is 2 which is the very first row then in the next row it will keep that '2' as long as the A column is empty until i reach a value (1) in the A column. When it reach the next non empty row in A column (row 7), then value in B now will keep that value (7) and it will keep that value all the way down until it reach the next non empty row in A , which is row 15. etc. Hope i can explain it clearly.
for now i only use basic formula in B2 cell :
=if( A2<>1, min( row(A2), indirect( "b" & (row(A2) -1) ) ) , row( A2) )
and then copy it down to other cells in B column. It works. But i'm just want to convert this into arrayformula() and got no luck. Does anyone know how to make this works using arrayformula ?

use:
=INDEX(VLOOKUP(ROW(A2:A), {2; FILTER(ROW(A3:A), A3:A<>"")}, 1, 1))

Try this:
=ArrayFormula(vlookup(row(A2:A24),query({row();ArrayFormula(value(substitute(A3:A24,1,row(A3:A24))))},"select * where Col1>0"),1,true))
It has a few stages:
First it takes row number from the first cell using row(). Then it substitutes all 1 cells in column A into corresponding row numbers. Then using query I remove empty or 0 values. I got a small table of:
2
7
15
19
Next stage is to take each row number from a2:a24 and vlookup through my table.
Using vlookup with 'true' parameter it returns nearest value from the table that is smaller than row number tested. So 2 returns 2, 3 returns 2, 4 returns 2, etc.

Related

C++ Search a 2D vector for elements surrounding a chosen element?

Stuck here in my assignment. I am working with 2D Vectors. What my professor wants us to do is write a program that has the user enter a size of a matrix (N X N) and print the matrix with random 1's and 0's, which I have done.
What I am stuck is that he wants to find "nonzero" elements around a certain element. For instance:
0 0 0
0 1 1
1 1 1
Now the user is asked to type in a row and column to (to locate an element) then search for nonzero values adjacent to that element. So if rows and columns start at 0, row 1 and column 1 holds the value "1" (the center of the matrix) and has 4 adjacent nonzero elements. I am not quite sure where to go from here. Would I use the find code? I am not sure how to limit that to the adjacent locations of one element.
Thank you
Hint: if you want to look at the adjacent elements, you can just shift each index by one position. For example, if the given (row, column) is (1, 1), the adjacent positions are (0, 1), (2, 1), (1, 0), (1, 2). You should make sure your code only reads indices in the range (0..N, 0..N).
This is your assignment and you should do your best to finish it. Go for it and make us proud!

Fill the Grid with three colors A and B and C

How to find the numbers of ways to fill a grid (3*n)array, with three colors A, B and C.
Under the following constraints:
1) All the n cells of the same row can't have the same color.
2) All the 3 cells of the same column can't have the same color.
Sample input : if n=2, then output or number of ways = 174.
Please explain the approach for this.
This answer given by sam29 on codefores.
We can solve this question using Inclusion-Exclusion principle. So, let's first consider only the first column of the matrix. We can easily deduce that there are 24 different ways to fill that column keeping in mind that we can't have the same letter in the complete column. Now, we can directly say that the total ways to fill the complete matrix will be 24^N (Name this value as X1). In this answer, we have made sure that all the column contains distinct alphabets. But we need to consider the cases a row contains the same letter. So, now we will use inclusion-exclusion principle.
Find the number of cases with one row same. Fix 'A' in the first row. Now, take only the first column and you can deduce that there are 8 distinct ways to fill the 2nd and the 3rd row of the first column keeping in mind we can't have the same letter in the complete column. So, now we can find the total number of ways to fill all the N rows as 8^N. Now, we can do the same thing with 'B' and 'C' in the first row and similarly, we can repeat the process for the 2nd and the 3rd row. So, the total number of ways will be 9*8^N (Name this value as X2).
Find the number of cases with two rows same (Name this value as X3). This is the trickiest part of the question. I'll explain this at last.
Find the number of cases with all the three rows same but we can't have the same letter in a single column. This is pretty easy and is equivalent to the number of ways to fill a single column and 3 rows. So, the answer is 24 for this scenario (Name this value as X4).
Now, the final answer will be X1-X2+X3-X4.
Now, coming back to the answer for 2nd scenario. So, we will try to find the answer for the case when the first row and second row is same and we can repeat that process for the 2nd row and 3rd row AND 1st row and 3rd row. Basically, we can multiply the answer we will calculate now with 3. Okay, now take only the first column. Now, you can see that there will be 2 scenarios, one will be when the first and second row contains the same letter and in that case, we have to place a different letter in the 3rd row because else we will violate our condition of the distinct column. So, the total number of ways in the first scenario will be 3*2^N (I have skipped some part but I have provided the exact reason and a little further thinking and you will get the solution). Now, for the next scene when the first and second row contains different letters. In that case, you can place any letter in the 3rd row. Again try to think a little more and you will get the answer as 6*3^N. So, the total answer will be 3*2^N + 6*3^N. And as I said before we need to multiply it by 3 (Number of ways to choose 2 rows from 3 rows). So, X3 will be 3*(3*2^N + 6*3^N).
The complexity is pretty direct, you can do precomputation or apply exponent function every time.
Thanks.
This is combinatorial question, for sure it is better to post questions like this on math.stackexchange.com.
A row can be in two different configurations: having two colors (ABA) and having three colors (ABC). If we have last row of some configuration, lets check possibilities for next row.
A | B B B C C
B | A A C A A
A | B C B B C
A | B B B C
B | A C C A
C | B A B B
Set:
A_n : number of dimension n matrices where last row is of ABA configuration,
C_n : number of dimension n matrices where last row is of ABC configuration,
X_n : number of dimension n matrices = A_n + C_n.
From upper list of possibile next row it holds:
A_n = 3 * A_(n-1) + 2 * C_(n-1) = 2 * X_(n-1) + A_(n-1)
C_n = 2 * A_(n-1) + 2 * C_(n-1) = 2 * X_(n-1)
=>
X_n = 4 * X_(n-1) + A_(n-1)
Result to question is X_n, for which calculation A_n is needed, and initial values are A_1=6, X_1=12.
Update:
Search in OEIS for values 2, 9, 41, 187 (upper sequence if colors are not important, real number divided by 6), produces sequence A020698. Sequence mentions similar problem, and suggests that upper recursion can be stated in simpler manner:
X_n = 4 * X_(n-1) + A_(n-1)
= 4 * X_(n-1) + A_(n-1) + X_(n-1) - X_(n-1)
= 5 * X_(n-1) + 2 * X_(n-2) + A_(n-2) - 4 * X_(n-2) - A_(n-2)
= 5 * X_(n-1) - 2 * X_(n-2)

OpenCV reshape function does not work properly

I am using OpenCV reshape function in order to reshape Mat of 25 rows and 1 column (so we have 25 data values) into Mat of 5 rows and 5 columns.
data = mu.reshape(5, 5);
When I look at my data variable in debugger, it has 5 rows but 1 column. If I print data at row(0) and col(0) it outputs all five values. So basically each row at col(0) contains 5 values.
My desired result is to get 5 rows and 5 columns where on each (row,col) will be one value.
Thank you in advance for your help.
You seem to have misinterpreted the meaning of arguments of the reshape() function.
According to the documentation the signature is
Mat Mat::reshape(int cn, int rows=0) const
With the following meaning of the arguments:
cn – New number of channels. If the parameter is 0, the number of channels remains the same.
rows – New number of rows. If the parameter is 0, the number of rows remains the same.
Note that the number of columns is implicit -- it's calculated from the existing matrix properties and the two parameters.
According to this, the code
data = mu.reshape(5, 5);
creates a 5-channel matrix of 5 rows and 1 column.
In order to reshape you matrix to a single channel 5x5 matrix, you have to do the following:
data = mu.reshape(1, 5);
Alternately, since the input matrix is already single channel, you can also use
data = mu.reshape(0, 5);

How to find the Range of Cell in excel - Qt?

I have cell's value as row and column.
Cells(1,10); //1 is row and 10 is column
Now i want to identify the Range of of that cell location.
J1; //where j is column and 1 is row
Is there any possibility to find out in QT?
Using the object model
Cells(5, 3).address(true,true,xlA1)
would return "$C$5"

Number of Items in a Column inside a grid

How do you find number of items in a column inside a grid?
I have a grid (listview control to be specific), and have some items.
Some times a given row might not be full. ANd can have values in fewer than maximum columns. I need to find Number of items in a given Column.
If the grid is like
1 2 3
4 5 6
7
and if input column is 1, then we need to output 3, and 2 for input of 2 or 3.
I have variables to for ItemCount, CoulmnCount and RowCount which track number of items, rows and columns.
A very rudimentar way would be something like this:
int iItemCount=0,iItemInColumn=0;
for(int iCol=0;iCol<iColumnCount;iCol++)
for(int iRow=0;iRow<iRowCount;iRow++,iItemCount++)
if(iCol==iInputCol && iItemCount<iTotalItems)
iItemInColumn++;
Can you guys think of any sophesticated way, which does not need loops? possible utilizing just 3 variables which I already have for tracking?
Assuming 0-based indexes:
def itemsInColumn(itemCount, columnCount, inputColumn):
lastItemColumn = (itemCount - 1) % columnCount
if inputColumn <= lastItemColumn:
return (itemCount + columnCount - 1) / columnCount
else:
return itemCount / columnCount
It depends on the total number of items (itemCount) and the number of columns (columnCount). It just computes itemCount / columnCount, and rounds up or down depending on whether the input column is less than or equal to the last item's column.
The computation "(itemCount + columnCount - 1) / columnCount" is just a trick for rounding up using integer division. In general, given positive integers a and b: ceil(a / b) = (a + b - 1) div b, where div is integer division.