C - how to split a matrix in 4 - c

What`s the easiest method to split a matrix in 4?
I have a nxn matrix, where n is multiple of 4;
http://i.stack.imgur.com/S4H2m.png
____________________
| | |
| | |
| 1st | 2nd |
| | |
|--------+----------
| | |
| 4th | 3rd |
| | |
|________|_________|
I don`t need to make a new matrix, only to get the ranges of i,j that refer to that new matrix;

1 st quadrant range of indices 0 - i/2 and 0 - j/2
2nd 0 - i/2 and j/2+1 - j
3rd i/2+1 - i and j/2+1 - j
4th i/2+1 - i and 0 - j/2

Maybe this could help you:
First matrix should go from: (0,0) - (n/2-1,n/2-1)
Second matrix should go from: (0,n/2) - (n/2-1,n-1)
3th matrix should go from: (n/2,n/2) - (n-1,n-1)
4th matrix should go from: (n/2,0) - (n-1,n/2-1)

Related

How to create array of given length and same number?

When using SUMSERIES I need to specify "the array or range containing the coefficients of the power series" but I want to make it so the number of elements is dynamic while the element itself (1) remains the same.
Example:
SUM FROM 0 TO N of x^1,5
(cell) Length of series N : 7 -- > SUMSERIES(1,5;0;1;{1,1,1,1,1,1,1})
But I should be able to change the seven for a 3 and get --> SUMSERIES(1,5;0;1;{1,1,1})
In Java for example you'd declare and instantiate the array --> int[] arr = new int[N];
And then fill in a loop --> for(int i = 0; i <arr.length; i++) {arr[i] = 1,5}
Thanks in advance and sorry if the explanation isn´t clear, it's my first time around hehe
this should work:
=SUMSERIES(1,5;0;1;SEQUENCE(1,[cell],1,0))
try:
=ARRAYFORMULA(SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1)))))
and then:
=INDEX(SUMSERIES(1,5; 0; 1; SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1))))))
In older version of Excel you can get this array using this (all of them are array formulas)
=INDEX(MUNIT(n),1,0)*0+x for horizontal array
=INDEX(MUNIT(n),0,1)*0+x for vertical array
Where:
n is dimension of the array
x is value of each item in the array
How it works:
MUNIT creates an identity matrix of size N
+---++---+---+---+---+---+
| || 1 | 2 | . | . | n |
+---++---+---+---+---+---+
+---++---+---+---+---+---+
| 1 || 1 | 0 | 0 | 0 | 0 |
| 2 || 0 | 1 | 0 | 0 | 0 |
| . || 0 | 0 | 1 | 0 | 0 |
| . || 0 | 0 | 0 | 1 | 0 |
| n || 0 | 0 | 0 | 0 | 1 |
+---++---+---+---+---+---+
Now we extract one (the first) row/column (n is set to 7 here)
=INDEX(MUNIT(7),1,0) for row extraction
=INDEX(MUNIT(7),0,1) for column extraction
And fill it with desired number (desired number here is 9 here)
=INDEX(MUNIT(7),1,0)*0+9 for row
=INDEX(MUNIT(7),0,1)*0+9 for column

How to generate long (up to 25 millions) random sequence of integers in C (with no repetition)?

I need to generate long (pseudo)random arrays (1000-25 000 000 integers) where no element is repeated. How do I do it since rand() function does not generate numbers long enough?
I tried to use this idea: array[i] = (rand() << 14) | rand() % length; however I suppose there is much better way that I don't know.
Thank you for your help.
You can use the Fisher-Yates shuffle for this.
Create an array of n elements and populate each element sequentially.
-------------------------
| 1 | 2 | 3 | 4 | 5 | 6 |
-------------------------
In this example n is 6. Now select a random index from 0 to n-1 (i.e. rand() % n) and swap the number at that index with the number at the top of the array. Let's say the random index is 2. So we swap the value at index 2 (3) and the one at n-1 (6). Now we have:
v
-------------------------
| 1 | 2 | 6 | 4 | 5 | 3 |
-------------------------
Now we do the same, this time with the upper bound of the index being n-2. Then we swap the value at that index with the value at index n-2. Let's say time we randomly get 0. So we swap index 0 (1) with index n-2 (5):
v
-------------------------
| 5 | 2 | 6 | 4 | 1 | 3 |
-------------------------
Then repeat. Let's say the next random index is 3. This happens to be our upper limit, so no change:
v
-------------------------
| 5 | 2 | 6 | 4 | 1 | 3 |
-------------------------
Next we get 0:
v
-------------------------
| 6 | 2 | 5 | 4 | 1 | 3 |
-------------------------
And finally 1:
v
-------------------------
| 6 | 2 | 5 | 4 | 1 | 3 |
-------------------------

Efficient algorithm for looping over all neighbor pairs (2 point cliques) in 2-D array

I need to loop over all (unordered) pairs of pixels in an image that are neighbors of each other without repetition. I am using an 8 point neighborhood. For example:
x,y| 0 1 2 3 4
---+---+---+---+---+---+
0 | | | | | |
+---+---+---+---+---+
1 | a | b | c | d | |
+---+---+---+---+---+
2 | e | f | g | h | |
+---+---+---+---+---+
3 | i | j | k | l | |
+---+---+---+---+---+
4 | | | | | |
+---+---+---+---+---+
The neighbors of pixel f are in the 3x3 square around it. Thus, g, for example, forms a 2 point clique with f. If I were to loop over all the rows and columns of the image, this clique would be counted twice, once when f is the center pixel and once when g is the center pixel. Similar inefficiencies would occur with the rest of the cliques.
So what I would like to do, is loop over all the cliques, rather than each pixel. If I were familiar with graph theory, I think some of the answers already given to similar questions would suffice, but as I am not, I would really appreciate any help that you can give with an efficient algorithm in layman's terms. Thanks in advance!
Loop the first point over all points. Inner loop the second point over the right, lower-left, lower, and lower-right neighbors (if they exist).

C program to find the number of onto functions and to display all the functions

I have to write a C program (for my Discrete Mathematics assignment) that finds the number of onto functions from set A (|A| = m) to set B (|B|=n) and to display all those functions. Number of onto functions I calculated using the code:
for(k=0; k<n; k++)
x = x + pow( -1, k) * combination( n, n - k ) * pow( ( n - k ), m);
Where combination is a function that finds the Number of possible combinations.
For example if A = {1,2,3}, B={a,b,c} then the number of onto functions evaluated from the formula is
3^3 - 3(2^3) + 3 = 6.
One possible solution is f = {(1,a),(2,b),(3,c)} [I know this is a solution].
But my problem is: How to display each and every solution!?
This is just a trivial example. But if m and n values are increased (provided m>=n) then the number of possible onto functions increases exponentially!!
For example if m=7 and n=4 there are 8400 functions!
I can't think of any method to display each and every function that exists between A and B.
I answered a similar problem sometime ago but m and n were equal m = n.(You must think recursively to solve this), by your comment I think the possible answers are: {(1,a)(2,b)(3,c)}, {(2,a)(3,b)(1,c)}, {(3,a)(1,b)(2,c)}, {(3,a)(2,b)(1,c)}, {(2,a)(1,b)(3,c)} and {(1,a)(3,b)(2,c)} then this is my recipe:
Set 2 arrays with their initial value let's call them letters and numbers.
*---*---*---* *---*---*---*
| a | b | c | <---letters. | 1 | 2 | 3 | <---numbers.
*---*---*---* *---*---*---*
Choose one of the arrays to be your pivot, I chose the letters, it will be statical.
*---*---*---* *---*---*---*
| a | b | c | <---STATIC. | 1 | 2 | 3 | <---DYNAMIC.
*---*---*---* *---*---*---*
Rotate the dynamic array counter-clockwise or clockwise as you wish, you must print the i element of numbers with the i element of letters.
*---*---*---* *---*---*---* *---*---*---*
| 1 | 2 | 3 | -(Print)-> | 2 | 3 | 1 | -(Print)-> | 3 | 1 | 2 |
*---*---*---* *---*---*---* *---*---*---*
So you get at this point: {(1,a)(2,b)(3,c)}, {(2,a)(3,b)(1,c)}, {(3,a)(1,b)(2,c)}, 3 are missing.
Swap the i element with the n element of the dynamic array.
*---*---*---* *---*---*---*
| 1 | 2 | 3 | ---------( Swap (0<->2) )-------> | 3 | 2 | 1 |
*---*---*---* *---*---*---*
Repeat the step 3.
*---*---*---* *---*---*---* *---*---*---*
| 3 | 2 | 1 | -(Print)-> | 2 | 1 | 3 | -(Print)-> | 1 | 3 | 2 |
*---*---*---* *---*---*---* *---*---*---*
So you get the missed subsets:{(3,a)(2,b)(1,c)}, {(2,a)(1,b)(3,c)} and {(1,a)(3,b)(2,c)}.
If you have more than 3 example 4. Easy: 1234 (rotate N times where N is the number of variables and print with each movement), swap 1 and 4 -> 4231 (Rotate and Print), swap 2 and 3 -> 4321 (Rotate and Print), swap 4 and 1 --> 1324 (Rotate and Print).
I hope this helped.

Weighted random integers

I want to assign weightings to a randomly generated number, with the weightings represented below.
0 | 1 | 2 | 3 | 4 | 5 | 6
─────────────────────────────────────────
X | X | X | X | X | X | X
X | X | X | X | X | X |
X | X | X | X | X | |
X | X | X | X | | |
X | X | X | | | |
X | X | | | | |
X | | | | | |
What's the most efficient way to do it?
#Kerrek's answer is good.
But if the histogram of weights is not all small integers, you need something more powerful:
Divide [0..1] into intervals sized with the weights. Here you need segments with relative size ratios 7:6:5:4:3:2:1. So the size of one interval unit is 1/(7+6+5+4+3+2+1)=1/28, and the sizes of the intervals are 7/28, 6/28, ... 1/28.
These comprise a probability distribution because they sum to 1.
Now find the cumulative distribution:
P x
7/28 => 0
13/28 => 1
18/28 => 2
22/28 => 3
25/28 => 4
27/28 => 5
28/28 => 6
Now generate a random r number in [0..1] and look it up in this table by finding the smallest x such that r <= P(x). This is the random value you want.
The table lookup can be done with binary search, which is a good idea when the histogram has many bins.
Note you are effectively constructing the inverse cumulative density function, so this is sometimes called the method of inverse transforms.
If your array is small, just pick a uniform random index into the following array:
int a[] = {0,0,0,0,0,0,0, 1,1,1,1,1,1, 2,2,2,2,2, 3,3,3,3, 4,4,4, 5,5, 6};
If you want to generate the distribution at runtime, use std::discrete_distribution.
To get the distribution you want, first you basically add up the count of X's you wrote in there. You can do it like this (my C is super rusty, so treat this as pseudocode)
int num_cols = 7; // for your example
int max;
if (num_cols % 2 == 0) // even
{
max = (num_cols+1) * (num_cols/2);
}
else // odd
{
max = (num_cols+1) * (num_cols/2) + ((num_cols+1)/2);
}
Then you need to randomly select an integer between 1 and max inclusive.
So if your random integer is r the last step is to find which column holds the r'th X. Something like this should work:
for(int i=0;i<num_cols;i++)
{
r -= (num_cols-i);
if (r < 1) return i;
}

Resources