How to do padding for a matrix - c

I am trying to do the padding for a basic matrix.
For example, I have a 3 * 3 matrix and I want to pad it to 7 * 7 matrix:
3x3 Matrix
1 2 3
4 5 6
7 8 9
Is there any efficient way to add 2 padding to on the matrix?
7x7 Matrix After Padding
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 2 3 0 0
0 0 4 5 6 0 0
0 0 7 8 9 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

Related

Consecutive elements of a vector in matlab

I am trying to display some value of n consecutive numbers of a vector (in this example, vector x).
x = [1
1
1
1
1
1
0
0
0
0
1
1
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
1
0
0
1
1
1
1
1
1
0
0
1
1
1
1
0
0
1
1
1
1
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
1
0
0
0
1
0
0
0
0
1
0
1
0
0
1
0
0
0
0
1
1
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
1
0
1
0
0
0
1
0
0
0
0
0
0
1
0
0
0
1
0
0
1
1
1
1
1
1
0
0
1
1
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0];
For example, I may want the first 3 values of 4 consecutive numbers which would be (3 values of 4 bits each):
1111
1100
0011
I may want the first 4 values of 2 consecutive numbers which would be (4 values of 2 bits each):
11
11
11
00
x is a double array. What would be an easy way to achieve this?
The simplest way is to reshape x to a matrix with your desired number of bits per value as the number of rows (MATLAB is column-major). For example, to get 4-digit values:
t = reshape(x,4,[]);
This will only work if the length of x evenly divides into 4. You could first crop x to the right number of elements to avoid errors where the length is not evenly divisible:
t = reshape(x(1:4*3),4,[]);
Now the transposed matrix t, converted to a string, looks like your desired output:
c = char(t.' + '0');
The output is a 3x4 char array:
'1111'
'1100'
'0011'
You can convert these binary representations back to numbers with bin2dec:
b = bin2dec(c);
The output is a 3-element double vector:
15
12
3

Looping through an array to check a condition

I'm using a FOR loop to loop through an array to check a condition but it seems to duplicate the variables I want to print.
Here's my code:
/*Printing the matrix*/
for(row=0;row<15;row++)
{
for(col=0;col<8;col++)
{
for(a=0;a<sizeof(row_sel)/(sizeof(row_sel[0]));a++)
{
if(row==row_sel[a] && col==col_sel[a])
{
printf("|%d|",matrix[row][col]);
}
else
{
printf(" %d ",matrix[row][col]);
}
}
}
printf("\n");
}
What I'm trying to print is:
|0| 6 7 3 6 6 9 5
0 0 0 0 0 0 0 0
2 8 8 4 8 4 0 0
0 0 0 0 0 0 0 0
5 0 8 7 9 5 5 3
0 0 0 0 0 0 0 0
2 7 8 5 3 3 8 6
0 0 0 0 0 0 0 0
6 6 4 2 9 6 2 1
0 0 0 0 0 0 0 0
9 4 0 6 6 7 0 4
0 0 0 0 0 0 0 0
4 3 8 9 0 2 2 7
0 0 0 0 0 0 0 0
0 3 6 7 8 3 5 |2|
But instead I get this output, I don't know why the values are duplicated,the loop should've ended or skipped the statement:
|0| 0 0 1 1 1 2 2 2 7 7 7 7 7 7 3 3 3 6 6 6 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 6 6 6 3 3 3 2 2 2 0 0 0 4 4 4 2 2 2 9 9 9
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 5 5 4 4 4 6 6 6 3 3 3 5 5 5 8 8 8 6 6 6 5 5 5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 5 5 7 7 7 1 1 1 9 9 9 1 1 1 2 2 2 0 0 0 4 4 4
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 9 9 7 7 7 3 3 3 6 6 6 8 8 8 0 0 0 3 3 3 8 8 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 5 5 2 2 2 6 6 6 5 5 5 2 2 2 7 7 7 5 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 5 5 7 7 7 8 8 8 3 3 3 2 2 2 1 1 1 1 1 1 6 6 6
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 9 9 6 6 6 9 9 9 7 7 7 2 2 2 6 6 6 2 2 2 5 |5| 5
Any tips to where I went wrong? Any help is quite appreciated.
Your problem:
You print each value of the matrix for every a, but you only want to print it once.
The solution:
You have to move the if-else statement out of the innermost loop:
for(row=0;row<15;row++)
{
for(col=0;col<8;col++)
{
bool foundMatch = false;
for(a=0;!foundMatch && a<sizeof(row_sel)/(sizeof(row_sel[0]));a++)
{
foundMatch = row==row_sel[a] && col==col_sel[a];
}
if(foundMatch)
{
printf("|%d|",matrix[row][col]);
}
else
{
printf(" %d ",matrix[row][col]);
}
}
printf("\n");
}
Why/How it works:
The inner loop will run until foundMatch is true, or a gets out of range.
foundMatch is set to true if there is any a for which row==row_sel[a] && col==col_sel[a] is true.
|%d| will print if foundMatch is true.

How to find elements in an array based on a search from another array

Imagine that i have two arrays:
a = [1 1 1 1 5 5 5 5 5 5 8 8;
1 1 1 3 5 5 5 5 5 8 8 8;
1 1 3 3 3 5 5 5 8 8 8 8;
1 3 3 3 3 3 5 8 8 8 8 8;
4 4 4 9 9 0 3 3 8 8 8 8;
4 4 4 9 0 0 3 3 3 3 8 8;
4 4 9 9 0 0 0 0 0 0 1 1;
4 9 9 9 0 0 0 0 0 0 1 1;
9 9 9 9 9 0 0 0 7 7 7 7];
b = [4 5 7];
I want ans like this :
ans =
0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1
The function ismember does exactly that:
ismember(a, b)
ans =
9×12 logical array
0 0 0 0 1 1 1 1 1 1 0 0
0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1
Not sure if this is the most efficient but this should work:
c = zeros(size(a));
for i = 1:numel(a)
if ismember(a(i), b(:))
c(i) = 1
end
end
Testing on some smaller arrays:
octave:1> a = [1 1 5 5 8 8;1 5 1 3 5 8]
a =
1 1 5 5 8 8
1 5 1 3 5 8
octave:2> b = [5 8]
b =
5 8
octave:3> c = zeros(size(a));
for i = 1:numel(a)
if ismember(a(i), b(:))
c(i) = 1
end
end
c =
0 0 0 0 0 0
0 1 0 0 0 0
.
.
.
c =
0 0 1 1 1 1
0 1 0 0 1 0
c =
0 0 1 1 1 1
0 1 0 0 1 1

How to sort an arrange value in vector in Matlab?

Assume that we have a vector as
A = [ 0 0 0 0 -1 2 -5 4 5 3 9 0 0 0 0 0]
How to sort a matrix with the value is increasing, and it could be become as
A = [0 0 0 0 -5 -1 2 3 4 5 9 0 0 0 0 0]
Thanks a lot,
You can use logical indexing and sort.
For example, assuming only 1 "island" of non-zeros:
A = [ 0 0 0 0 -1 2 -5 4 5 3 9 0 0 0 0 0];
A(A~=0) = sort(A(A~=0));
Returns:
>> A
A =
0 0 0 0 -5 -1 2 3 4 5 9 0 0 0 0 0

How to detect connected components in a 2D array?

As mentioned the title above. I want to find out whether there are how many components in a 2D Array. Whereas, components are made by 1 numbers and there are only 0 and 1 number in the array.
I implemented this problem by using DFS (Deep First Search) algorithm with recursive calls and an array to mark cell visited.
However, I want to implement this problem with another way without using recursion, stack, queue, struct... Only using for/while function are allowed.
Example:
Array data:
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
0 0 1 1 1 0 1 1 0 1 0 0 0 0 0 1
0 0 1 0 1 0 1 1 0 1 0 1 1 1 0 1
0 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1
0 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1
0 0 1 1 1 0 0 0 0 1 0 1 0 1 0 1
0 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1
0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1
0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0
0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0
0 1 0 1 1 1 1 1 0 0 1 1 1 1 0 0
0 1 0 1 0 1 0 1 0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
Array after determined components with specific labels.
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
0 0 2 2 2 0 3 3 0 1 0 0 0 0 0 1
0 0 2 0 2 0 3 3 0 1 0 4 4 4 0 1
0 0 2 0 2 0 0 0 0 1 0 4 0 4 0 1
0 0 2 0 2 0 0 0 0 1 0 4 0 4 0 1
0 0 2 2 2 0 0 0 0 1 0 4 0 4 0 1
0 0 0 0 0 5 5 5 0 1 0 4 4 4 0 1
0 0 0 0 0 5 0 5 0 1 0 0 0 0 0 1
0 0 0 0 0 5 5 5 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 6 6 6 6 6 0 0 0 0 7 7 7 7 0 0
0 6 0 0 0 6 0 0 0 0 7 0 0 7 0 0
0 6 0 6 6 6 6 6 0 0 7 7 7 7 0 0
0 6 0 6 0 6 0 6 0 0 7 7 7 7 0 0
0 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0
0 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0
Thank you in advance.
I guess you could iterate through the matrix, and check the neighbours for each cell, and copy the value of the neighbour if that is > 0 or set a new value if all the neighbours are 0. In pseudocode:
comp = 1
for i = 0 to n:
for j = 0 to n:
for nei : neighbours(i, j):
if nei > 0:
m[i,j] = nei
break
m[i,j] = comp
comp++
And neighbours are the 4 (or 2) adjacent neighbouring cells to (i, j)

Resources