Distance of nearest cell having - binary

Given a binary grid of n*m. Find the distance of the nearest 1 in the grid for each cell.
The distance is calculated as |i1 - i2| + |j1 - j2|, where i1, j1 are the row number and column number of the current cell, and i2, j2 are the row number and column number of the nearest cell having value 1.
Input: grid = {{0,1,1,0},{1,1,0,0},{0,0,1,1}}
Output: {{1,0,0,1},{0,0,1,1},{1,1,0,0}}
Explanation: The grid is-
0 1 1 0 
1 1 0 0 
0 0 1 1 
0's at (0,0), (0,3), (1,2), (1,3), (2,0) and
(2,1) are at a distance of 1 from 1's at (0,1),
(0,2), (0,2), (2,3), (1,0) and (1,1)
respectively.

Related

Algorithm for selecting tiles outwards center point in a 512*512 map

I've got a specific problem. My data (map) in mysql is as follows
id table_row table_col tile_type
1 1 1 0
2 2 1 0
3 3 1 0
... ... ... 0
512 512 1 0
513 1 2 0
514 2 2 0
515 3 2 0
... ... ... 0
... 512 2 0
... 1 3 0
... 2 3 0
... 3 3 0
... ... ... 0
... 512 3 0
... 1 4 0
Map is 512*512. I need to come up with an algorithm that selects tiles from the centre(or near centre 256*256) point. So it should look something like
256*256 first - once selected we can update tile_type to 1
255*256 second - update tile_type to 1
256*255 third - update tile_type to 1
257*256 fourth - update tile_type to 1
256*257 fifth - update tile_type to 1
etc. or similar, but it has to start filling in tiles from centre outwards in all directions (can be random). Any ideas appreciated
Your question lacks a few details, but I am assuming you are asking a means of generating an id that is close to the center of your 512x512 grid.
It appears your grid is enumerated in a particular manner: each column is enumerated in increasing order of table_row values, and the enumeration of columns is done in increasing order of table_col values.
Consequently, we can already know the id of the cell for which the table_row and table_col values are 256: it is 255 x 512 + 256. That is correct, because there are 255 full columns that were enumerated before enumeration started for table_col value 256, and each of those columns had 512 rows in them. Finally, within this column, we are interested in row #256.
A more generalized version of this would look like below.
((num_cols + 1) / 2 - 1) * num_rows + (num_rows + 1) / 2
You don't need to care all that much about the +1s and -1s: they are just a numerical hack to handle odd num_rows and num_cols values.
Anyways, to introduce a proximity measure, you can just use two random variables. A random variable P can represent the distance to the center in terms of colums. (i.e. how far the table_col of the point with the generated id will be from the table_col value of the center of the grid) Another random variable Q can represent the distance to the center in terms of rows.
((num_cols + 1) / 2 - 1 + P) * num_rows + ((num_rows + 1) / 2 + Q)
Then you can just generate values for P and Q based on your needs, and get the id of a cell that is P colums and Q rows away from the center of the grid.
Try Below query.
SELECT (MAX(t.`row`+1)/2), (MAX(t.`column`+1)/2) INTO #max_row, #max_col
FROM tiles t;
SELECT t.`row`, t.`column`, ceil(IF(ABS(#max_row - t.`row`) < ABS(#max_col - t.`column`), ABS(#max_col - t.`column`), ABS(#max_row - t.`row`))) as tbl_order
FROM tiles t
ORDER BY 3

Custom number sequence formatting

The system I am working with has a numbering system where the numbers 0-999 are represented by the usual 0-999, but 1000 is represented by A00, followed by A01, A02, A03, etc, 1100 being B00 etc.
I can't think of a way to handle this in T-SQL without resorting to inspecting individual digits with huge case statements, and there must be a better way than that. I had thought about using Hexadecimal but that's not right.
DECLARE #startint int = 1,
#endint int = 9999;
;WITH numbers(num)
AS
(
SELECT #startint AS num
UNION ALL SELECT num+1 FROM numbers
WHERE num+1 <= #endint
)
SELECT num, convert(varbinary(8), num) FROM [numbers] N
OPTION(MAXRECURSION 0)
With this 999 is now 3E7, where it should just be 999.
This currently produces this:
Number Sequence
0 0x00000000
1 0x00000001
...
10 0x0000000A
...
100 0x00000064
...
999 0x000003E7
1000 0x000003E8
What I'm looking for:
Number Sequence
0 000
1 001
...
10 010
11 011
12 012
...
999 999
1000 A00
1001 A01
...
1099 A99
1100 B00
1101 B01
1200 C00
I need this to work in SQL Server 2008.
You can use integer division and modulo to separate the hundreds part from the tens.
After that, you can add 64 to the quotient to get an ASCII value starting from A.
create function function dbo.fn_NumToThreeLetters(#num integer)
RETURNS nchar(3)
AS
begin
RETURN (SELECT (case
when #num/1000 >0 then
CHAR(( (#num-900)/100) +64)
+ replace(cast( #num %100 as nchar(2)),' ','0')
else cast(#num as nvarchar(3))
end)
)
END
select dbo.fn_NumToThreeLetters(1100)
-------
B00
select dbo.fn_NumToThreeLetters(999)
-------
999
The first when clause ensures that the conversion is applied only if a number is above 1000. If it is, subtract 900 then divide by 100, so we get a number that starts from 1 for 1000, 2 for 1100, etc.
Add 64 to it to get an ASCII starting from A and convert it back to a character with CHAR.
The remainder just needs to be converted to a 2-digit nchar, where spaces are replaced with 0.
This will work only up to 3500. The question doesn't specify what should be done with larger numbers

How to create a histogram using MySQL

I am trying to create a histogram data using following query:
SELECT FLOOR(Max_Irrad/10) AS bucket, COUNT(*) AS COUNT
FROM marctest.test_summarynimish
where Lcu_name='Allegro'
and Lcu_Mode='Standard'
GROUP BY bucket;
following is the result that i am getting:
bucket count
0 3
4 3
5 12
7 6
8 3
10 3
now the bucket field is the range or bin used in the histogram. I want to create a bucket values with consistent range, for eg starting from 0,4,8,12.... and so on.. Is there any way to achieve this in mysql?
This is how I am expecting to have as result:
bucket count
0 3
4 21
8 6
I think we can use the following general form to create a general histogram:
select (x div 4) * 4 as NewX, count(*) as NewY from histogram
group by NewX
Where x is the real x value of the x axis and count(*) is the real y value. The number 4 is the size amount of the x values we want to group. This means we will group all x values in groups of 4 (e.g.: group 1 is 0, 1, 2, 3; group 2 is 4, 5, 6, 7, and so on). The count of each item in the group will become the NewY value
You can play with this here
Applying this logic to your query this would be:
select (floor(Max_Irrad/10) div 4) * 4 as NewX, count(*) as NewY
from marctest.test_summarynimish
where Lcu_name='Allegro' and Lcu_Mode='Standard'
group by NewX
Let me know if you have any trouble or doubt about this.
Just make your buckets bigger by dividing Max_Irrad by 40 instead of 10.

Define quadrant based on positive/negative values in two columns

I have a data set with two columns of positive and negative numbers. I would like to create a third column that reflects which quadrant they would appear in if plotted in Cartesian space.
For example, if Column A is positive, and Column B is positive, then Column C would record "I." If column A is negative, and Column B is negative, then Column C would record "III," and so on.
I suspect I can do this with an if else function and then loop or apply it across rows in the data set, but my attempts to write the if else have so far failed.
Well, the following would give you values between 1 and 4:
C <- (A<0) + (B<0)*2L + 1L
This transforms the whole column in one go. The magic lies in that FALSE/TRUE is treated as 0/1, and you can do math on it. By using 2L and 1L instead of 2 and 1, you keep the result as integers instead of forcing a coercion to doubles (which is slower and takes more memory).
Then assuming you want to map to these quadrants:
+B
|
II | I
-A -----+---- +A
III | IV
|
-B
You could use this (updated to use a data.frame):
# Sample data.frame with columns a & b
d <- data.frame(a=c(1,-1,-1,1), b=c(1,1,-1,-1))
quadrantNames <- c('I', 'II', 'IV', 'III') # Your labels...
d <- within(d, c <- quadrantNames[(a<0) + (b<0)*2L + 1L])
head(d) # print data
a b c
1 1 1 I
2 -1 1 II
3 -1 -1 III
4 1 -1 IV
...and if you want the quadrants mapped differently, just change the order of the labels in quadrantNames.

SQL sum round numbers and count decimal

How to sum integers as they are and treat floating numbers as 1.
From the table given below the expected result is:
1 + 1 + 1 + 5 = 8
colum1 colum2
aa 1
bb 0.5
cc 3.66
dd 5
You can compare each number to its floored value to check if it is a decimal or not, and then use a case expression to treat decimals as 1:
SELECT CAST(SUM(CASE number WHEN FLOOR(number) THEN number ELSE 1 END) AS INTEGER)
FROM mytable;