I'm drawing a HUGE blank here.
Everything I've found is about getting an index from a given row and column, but how do I get a row and a column from an index?
The row is easy: (int)(index / width).
My brain is suffering massive bleed trying to compute the column.
Shame on me.
For a zero-based index, the two operations are, where width is the width of the structure:
row = index / width
column = index % width
Those are for C using integers, where the division rounds down and the % modulo operator gives the remainder. If you're not using C, you may have to translate to the equivalent operations.
If you don't have a modulo operator, you can use:
row = index / width
column = index - (row * width)
Paxdiablo's answer is the right one. If someone needs the reverse process to get index from row and column:
index = (row * width) + column
Swift 4
typealias ColumnRowType = (column:Int, row:Int)
func indexToColumnRow(index:Int, columns:Int) -> ColumnRowType
{
let columnIndex = (index % columns)
let rowIndex = /*floor*/(index / columns) // we just cast to an `Int`
return ColumnRowType(columnIndex, rowIndex)
}
In java, with a column offset of 1, this is a way to do it
int offset=1;
column = Math.floorDiv(location-offset , 3)+offset;
row = ( (location-offset) %3 )+offset ;
Related
I want to use Power Query to extract by field(field is [Project]), then get the top 3 scoring rows from the master table for each project, but if there are more than 3 rows with a score of over 15, they should all be included. 3 rows must be extracted every time as minimum.
Essentially I'm trying to combine Keep Rows function with my formula of "=if(score>=15,1,0)"
Setting the query to records with score greater than 15 doesn't work for projects where the highest scores are, for example, 1, 7 and 15. This would only return 1 row, but we need 3 as a minimum.
Setting it to the top 3 scores only would omit rows in a table where the highest scores are 18, 19, 20
Is there a way to combine the two function to say "Choose the top 3 rows, but choose the top n rows if there are n rows with score >= 15
As far as I understand you try to do following (Alexis Olson proposed very same):
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
group = Table.Group(Source, {"Project"}, {"temp", each Table.SelectRows(Table.AddIndexColumn(Table.Sort(_, {"Score", 1}), "i", 1, 1), each [i]<=3 or [Score]>=15)}),
expand = Table.ExpandTableColumn(group, "temp", {"Score"})
in
expand
Or:
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
group = Table.Group(Source, {"Project"}, {"temp", each [a = Table.Sort(_, {"Score", 1}), b = Table.FirstN(a, 3) & Table.SelectRows(Table.Skip(a,3), each [Score]>=15)][b]}),
expand = Table.ExpandTableColumn(group, "temp", {"Score"})
in
expand
Or:
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
group = Table.Group(Source, {"Project"}, {"Score", each [a = List.Sort([Score], 1), b = List.FirstN(a,3)&List.Select(List.Skip(a,3), each _ >=15)][b]}),
expand = Table.ExpandListColumn(group, "Score")
in
expand
Note, if there are more columns in the table you want to keep, for first and second variants you may just add these columns to last step. For last variant you haven't such option and the code should be modified.
Sort by the Score column in descending order and then add an Index column (go to Add Column > Index Column > From 1).
Then filter on the Index column choosing to keep values less than or equal to 3. This should produce a step with this M code:
= Table.SelectRows(#"Added Index", each [Index] <= 3)
Now you just need to make a small adjustment to also include any score 15 or greater:
= Table.SelectRows(#"Added Index", each [Index] <= 3 or [Score] >= 15)
I am building a function which will allow the user to choose a cell in a dynamically created table, by passing in an integer.
I.e., if I have a 3x3 grid, and the user passes in the number 4, the program should know that he wants the 1st cell in the second row. (the cells will be counted by rows)
As I mentioned, the table is created dynamically and can be any size.
I can do this with a bunch of if statements, but I was wondering if there is maybe an algorithm to figure this out easily.
(P.S. I am using a very basic programming language, so please, no fancy pythonic math functions... :) )
All you need to do is divide by columns and find the remainder. Something like this:
input = 4
row = floor(input / columns)
column = input % columns
Assuming the output should be 1 based index
input = n, k // n * n grid, k number
row = floor( (k - 1) / n ) + 1;
col = k % n;
if(col == 0) {
col += n;
}
// print row, col
I've got an array of N elements.
Need to place them on the screen like a set of groups, where each group is a 4x4 table of elements. Width of a table is TW = 320px. Height is TH = 480px.
In local coordinates of each table:
the gap between columns must be equal; the gap between first column and left border must be equal with the gap between 4th column and right border and must be equal bordersGapX = 60px.
the gap between rows must be equal rowsGapY = 60px; firs row is starting at firstRowY 150px.
In global coordinates of the screen:
tables must be placed one near another with the gap of tablesGapX = 300 px;
Hope related picture helps...
Now we need to set X and Y position for each element in the loop just with the equation - no if operators or inner loops...
I really don't remember how, but I ended up with something like this, and it works:
for i=1,N do
element.x, element.y =
(((i%4)==0) and (TW-borderGapX) or ((i%4==1) and borderGapX or borderGapX+((TW-borderGapX*2)/3)*((i%4)-1) ))+math.floor(i/16)*tablesGapX - ((((i%4==0) and (i%16==0))) and tablesGapX or 0),
firstRowY+math.floor((i-1)/4)*rowsGapY - (math.floor(i/16)*rowsGapY*4) + ((((i%4==0) and (i%16==0))) and rowsGapY*4 or 0)
end
Somebody please help me simplify that!!!
Simplifying readability can be done as follows. You start be defining which table-number and which position-number each element has:
element.tableid = math.floor(i/16)
element.position = i - element.tableid
And then you use helping functions
for i=1,N do
element.tableid = math.floor(i/16)
element.position = i - element.tableid
element.x = compute_offsetxoftable(element.tableid)
element.y = compute_offsetyoftable(element.tableid)
element.x = element.x + compute_offsetxwithintable(element.position)
element.y = element.y + compute_offsetywithintable(element.position)
end
The functions I just implicitly define could even be a mapping table...
I have a list of numbers. Instead of painting them all in one row I am painting the list in rows of 5.
Now I can select one number and from there move left, right, up or down.
In this list of 15 numbers (indexed 0 to 14) I have selected index 11, coloured red.
If I move left I have to subtract 1 from the index of my selection. If I move right I add 1. Down means I add 5 and up means I subtract 5.
However, if I go down when I am in the bottom-most row, I want to end up in the first row, as such:
The math / algorithm for that is simple:
index += 5;
if (index > list.size() ) index = index % 5; // % is modulo
//So, since I start with index 11: (11 + 5) % 5 = 1, which is the index of 01.
However, I cannot seem to figure out what to do when I am going from the top-most row up, which takes me to the bottom-most row. (From 01 I would end at 11)
If I have a list of exactly 15 items, then I could simply do:
index -= 5;
if (index < 0) index += index.size();
//So: 1 - 5 = -4
// -4 + 15 = 11.
But if my list is not divisible by 5, then this does not work.
So I am looking for an algorithm that would solve this problem in all cases, including when the size of a list is not divisble by the length of its rows.
This can probably be optimized further, but here's one approach:
var fullRows = list.Length / NUM_COLUMNS; //using integer division
var maxPos = fullRows * NUM_COLUMNS + currentIndex;
return maxPos < list.Length ? maxPos : maxPos - NUM_COLUMNS;
What this does is gets the number of full rows then starts by assuming there is another row after it. It then checks if that position really exists, and if not it backs off a row to be inside the final full row.
I wish to ask if anybody out there knows how to partition an image into 8 different rows and 1 column? I have tried using mat2cell() and using the demo on their wiki as a reference, I tried partitioning the image into 8 rows, however not all image partition rows are displayed.
If you see the image below, 2, 4, 6, 8 is not displayed. I am also not sure why is it of 16 blocks.
Can somebody help me check my code? I am not really used to the MatLab syntax and language. I trying my best to understand now.
My code for splitting the blocks are as follows:
blockSizeR = 50; % Rows in block.
blockSizeC = 512; % Columns in block.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, 1, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
end
I am new to MatLab, so is there is a simpler method to do this that I missed out, please do suggest or better still, if there are references that I can refer to. Many thanks (:
If you know the dimensions of your matrix, you can do the math to figure out how to divide the number of rows into 4 equal parts:
e.g. If: size(rockinsMatrix) == [10 20] (a 10row x 20column) matrix,
then you could split it into a set of 4 sub-matrices, two with 3 rows, and 2 with 2 columns.
If you want the matrices in a cell array then you can do that at that time.
I managed to solve already, the error lies in the for loop. I changed the for r = 1 : numPlotsR into r = 1 : (number of rows I want) for c = 1 : numPlotsC into c= 1: 1(as I only want one column), and used subplot(8,1,k) or (8,2,k) where k is the plot index. Just answering this in case anybody encounter such problem in future and want to use my code as a reference. Cheers!