C - Populate matrix with some density - c

I've got this code to populate matrix with 0/1 values and RHO density. I need the same for values from 0 to 2. I mean, the percentage of zeros should be the same, but other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho;
}
}
The only thing I've been able to do is something inelegant like this. This leaves zero/non zero percentage inalterate and random modifies the 1 cells:
...
if(grid[cur][i][j] > 0) {
grid[cur][i][j] += rand()%2;
}

I think this code will create 0 with RHO density and other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho ? 0 : rand() % 2 + 1;
}
}

Related

Convolution operation without conditional loop

I am writing a convolution operation for a filter and a signal. The accumulation operation holds true only for the condition "j - k" is not < 0. Is there a way to remove this condition and try to split the loops to avoid the conditional clause.
for (i = 0; i < RBs ; i++) // Over Resource Blocks
{
for (j = 0; j < (IFFT_Len + Fil_Len -1); j++) ​// Over Output Length
{
acc = 0;
for (k = 0; k < Fil_Len; k++) // over conv operation
{
if (j-k >= 0)
{
acc += Filter[k + (i * fil_data)] * IFFT[j - k + (i * ifft_data)];
}
}
x[j] = acc;
}
UFMC_sig += x;
}

Print repeating diamond shapes with custom rows, columns and length

I am new to C, and I am trying to print diamond shapes according to the rows(2~10), columns(2~10) and the length(3, 5, 7, 9) of the diamond input from the user.
Using the code below I can print diamond and number of diamonds correctly, but I just can't get the correct distance between them.
void printDiamondWith(int diamondlength, int numberOfDiamonds) {
int i, j, k;
int star, space;
star = 1;
space = diamondlength;
for (i = 1; i < diamondlength * 2 - 1; i++) {
for (k = 0; k < numberOfDiamonds; k++) {
for (j = 0; j < space; j++) {
printf(" "); // Print the distance for the previous star
}
for (j = 1; j < star * 2; j++) {
printf("*");
}
for (j = 0; j < space; j++) {
printf(" "); // Print the distance for the next star
}
}
printf("\n");
// Check if length is equal 3, else length -1 to get the correct rows of second half of the diamond
if (diamondlength == 3) {
// Loops until the first half of the diamond is finished, then reverse the process to print the second half
if(i < (diamondlength - diamondlength / 3)) {
space--;
star++;
} else {
space++;
star--;
}
} else if (diamondlength >= 3) {
if (i < (diamondlength - 1 - diamondlength / 3)) {
space--;
star++;
} else {
space++;
star--;
}
}
}
}
Actual running result:
Expected result:
Your formulas for calculating the space is off. It works for me when I change this
space = diamondlength;
to this
space = diamondlength/2+1;
And this
for (k = 0; k < numberOfDiamonds; k++) {
for (j = 0; j < space; j++) {
to this:
for (k = 0; k < numberOfDiamonds; k++) {
for (j = 0; j < space-1; j++) {
In such situations I recommend hardcoding the variable for different parameters and write down what the variable has to be for what parameter so you can try to find a function that maps the parameter to the value. For instance I saw that as diamondlength increased, the space error also increased, so the relation between parameter and variable can't be one to one.

Inverse of a binary matrix in C

I have a binary matrix (zeros and ones) D[][] of dimension nxn where n is large (approximately around 1500 - 2000). I want to find the inverse of this matrix in C.
Since I'm new to C, I started with a 3 x 3 matrix and working around to generalize it to N x N. This works for int values, however since I'm working with binary 1's and 0's. In this implementation, I need unsigned int values.
I could find many solutions for int values but I didn't come across any solution for unsigned int. I'd like to find the inverse of a N x N binary matrix without using any external libraries like blas/lapack. It'd be great if anyone could provide a lead on M x N matrix.
Please note that I need inverse of a matrix, not the pseudo-inverse.
/* To find the inverse of a matrix using LU decomposition */
/* standard Headers */
#include<math.h>
#include<stdio.h>
int main() {
/* Variable declarations */
int i,j;
unsigned int n,m;
unsigned int rows,cols;
unsigned int D[3][3], d[3], C[3][3];
unsigned int x, s[3][3];
unsigned int y[3];
void LU();
n = 2;
rows=3;cols=3;
/* the matrix to be inverted */
D[0][0] = 1;
D[0][1] = 1;
D[0][2] = 0;
D[1][0] = 0;
D[1][1] = 1;
D[1][2] = 0;
D[2][0] = 1;
D[2][1] = 1;
D[2][2] = 1;
/* Store the matrix value for camparison later.
this is just to check the results, we don't need this
array for the program to work */
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++) {
C[m][j] = D[m][j];
}
}
/* Call a sub-function to calculate the LU decomposed matrix. Note that
we pass the two dimensional array [D] to the function and get it back */
LU(D, n);
printf(" \n");
printf("The matrix LU decomposed \n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", D[m][j]);
}
printf("\n");
}
/* TO FIND THE INVERSE */
/* to find the inverse we solve [D][y]=[d] with only one element in
the [d] array put equal to one at a time */
for (m = 0; m <= rows-1; m++) {
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[m] = 1;
for (i = 0; i <= n; i++) {
x = 0;
for (j = 0; j <= i - 1; j++){
x = x + D[i][j] * y[j];
}
y[i] = (d[i] - x);
}
for (i = n; i >= 0; i--) {
x = 0;
for (j = i + 1; j <= n; j++) {
x = x + D[i][j] * s[j][m];
}
s[i][m] = (y[i] - x) / D[i][i];
}
}
/* Print the inverse matrix */
printf("The Inverse Matrix\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", s[m][j]);
}
printf("\n");
}
/* check that the product of the matrix with its iverse results
is indeed a unit matrix */
printf("The product\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
x = 0;
for (i = 0; i <= 2; i++) {
x = x + C[m][i] * s[i][j];
}
//printf(" %d %d %f \n", m, j, x);
printf("%d \t",x);
}
printf("\n");
}
return 0;
}
/* The function that calcualtes the LU deomposed matrix.
Note that it receives the matrix as a two dimensional array
of pointers. Any change made to [D] here will also change its
value in the main function. So there is no need of an explicit
"return" statement and the function is of type "void". */
void LU(int (*D)[3][3], int n) {
int i, j, k;
int x;
printf("The matrix \n");
for (j = 0; j <= 2; j++) {
printf(" %d %d %d \n", (*D)[j][0], (*D)[j][1], (*D)[j][2]);
}
for (k = 0; k <= n - 1; k++) {
for (j = k + 1; j <= n; j++) {
x = (*D)[j][k] / (*D)[k][k];
for (i = k; i <= n; i++) {
(*D)[j][i] = (*D)[j][i] - x * (*D)[k][i];
}
(*D)[j][k] = x;
}
}
}
This is just a sample example that I tried and I have -1 values in the inverse matrix which is my main concern. I have 1000 x 1000 matrix of binary values and the inverse should also be in binary.
The matrix:
1 1 0
0 1 0
1 1 1
The matrix LU decomposed:
1 1 0
0 1 0
1 0 1
The Inverse Matrix:
1 -1 0
0 1 0
-1 0 1
The product:
1 0 0
0 1 0
0 0 1

Array function (image processing project)

I faced an issue of my assignment related the array function.
I want to make a border element with '0' at first three and last three rows and columns. Firstly , I able to generate the number of 256X256 size (array[256][256]).
Then, after the first array I generated that I need to do some condition .For example,
For element value <127, subtract 20 from the value.
For element value >127, add 20 to the value.
If the value of any element is <0 after the operation then assign the value 0 to it.
If the value of any element is >255 after the operation then assign the value 255 to
it.
The problem is when I generated again, the "0" border of element become different. How to i solve it to be like first array as seen like "0" border of element?
Below is my C++ code.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int inputimage[256][256], modifinputimage[256][256];
int i, j;
char ch;
for (i = 0; i < 256; i++)
{
if (i < 3)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 253)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 3 && i <253)
{
for (j = 0; j < 256; j++)
{
if ((i >= 3 && j < 3) || (i<253 && j >= 253))
{
printf("0\t");
}
if (j >= 3 && j < 253)
{
inputimage[i][j] = rand() % 256;
printf("%d\t", inputimage[i][j]);
}
}
}
}
printf("\nProceed to Contrast Adjustment ? (Press ENTER to continue)*** \n\n\n\n");
ch = getche();
for (i = 0; i < 256; i++)
{
if (i < 3)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 253)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 3 && i <253)
{
for (j = 0; j < 256; j++)
{
if ((i >= 3 && j < 3) || (i<253 && j >= 253))
{
printf("0\t");
}
if (j >= 3 && j < 253)
{
if (inputimage[i][j] < 127 && inputimage[i][j] >= 20)
{
modifinputimage[i][j] = inputimage[i][j] - 20;
printf("%1d\t", modifinputimage[i][j]);
}
if (inputimage[i][j] > 127 && inputimage[i][j] <= 235)
{
modifinputimage[i][j] = inputimage[i][j] + 20;
printf("%1d\t", modifinputimage[i][j]);
}
if (inputimage[i][j] <= 0)
{
modifinputimage[i][j] = inputimage[i][j];
printf("0\t");
}
if (inputimage[i][j] >= 255)
{
modifinputimage[i][j] = inputimage[i][j];
printf("255\t");
}
}
}
}
}
}
In your modification you need to use the approach of accessing the array by rows and columns by using two for loops at the start to have the position of both row and column. For Example
for (int i = 0; i < 256; i++){
for(int j = 0; j < 256; j++){
}
}
this way you are going through all the rows and columns and inside the above condition use
if(i <= 3 || i >= 253){
//here u can use the nested loop to print 0's for both first and last 3 rows
printf("0\t");
}
// similarly for columns you can use this
else if(j <= 3 || j >=253){
printf("0\t");
}
else if(i > 3 && i < 253 && j > 3 && j < 253){
//here your modification rules
}
hope this helps

Summarizing all points around a center point?

for the purpose of smoothing an image, I need to summarize all pixels around a given center pixel and then build the average, what's basically my new pixel then.
There are like two problems now:
1) What's a good way to summarize them? and
2) How can I best avoid the corner pixels?
This is what I did so far, but I don't think it's any good.
for(i = 0; i < image1->nx; i++)
{
for(j = 0; j < image1->ny; j++)
{
if(i == 0 || j == 0 || i == image1->nx - 1 || j == image1->ny - 1)
{
image2->image[i][j] = image1->image[i][j];
}
else
{
int average = 0;
average += image1->image[i][j];
average += image1->image[i+1][j];
average += image1->image[i][j+1];
average += image1->image[i+1][j+1];
average += image1->image[i-1][j];
average += image1->image[i-1][j+1];
average += image1->image[i-1][j-1];
average += image1->image[i][j-1];
average += image1->image[i+1][j-1];
average /= 9;
image2->image[i][j] = average;
}
}
}
My struct in C is something like this:
struct pgm_image
{
int nx; // row number
int ny; // cell number
unsigned char image[N1][N2]; // image information
};
This looks right. To shorten it a bit, I guess you could use a for loop.
int min = -1;
int max = 1;
int average = 0;
int amount = 0;
for(int k = min; k <= max; k++){
for(int l = min; l <= max; l++){
amount++;
average += image1->image[i+k][j+l];
}
}
average /= amount;
image2->image[i][j] = average;
It still looks messy, but this way you can alter the smooth "radius" by changing the min and max variables.

Resources