Fill incomplete holes of 3D matrix with Matlab [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a possibility to fill holes in a 3D matrix that aren't complete holes with Matlab?
I have tried imfill but obviously the incomplete holes were not detected as holes.
Thanks in advance.
e.g in 2D:
what I have
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 1 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 1 0 0 0
what I want:
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
0 0 1 1 1 0 0
0 0 0 1 0 0 0

You could try to use imclose. Imclose actually tries to fill "gaps", but not in the same global way as imfill -- it's only a very local filling.
se = strel('disk',2);
imclose(M, se)
This code works like a charm on the example you gave. Because the matrix is small it actually fills it, but a bigger hole would still require an imfill after the imclose.
I don't know if it will work on any sort of holes you may have in your matrix, especially for complex concave shapes. You may have to play with the structural element strel, both type and size. Hope this helps.

Related

I want element (3,2) from a matrix with size (4,4) to try the values of the first column from another matrix with size (3,3) one by one [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
For example: If I have matrix A and Matrix B:
A =
7 4 1
4 5 6
3 6 9
B = zeros(4,4)
B =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
I want I want element (3,2) from matrix B with size (4,4) to try the values of the first column from matrix A with size (3,3) one by one and at each time produce the new matrix. So, the output will be:
B =
0 0 0 0
0 0 7 0
0 0 0 0
0 0 0 0
B =
0 0 0 0
0 0 4 0
0 0 0 0
0 0 0 0
B =
0 0 0 0
0 0 3 0
0 0 0 0
0 0 0 0
How can I do this ?
A simple way would be to turn the 3D matrix to 1D vector. You now can write a function that loops over this vector and builds a different permutation of it each time. From each permutation you can reconstruct a 3D matrix.
How many possible permutations are there for a vector of size N ?
Answer is: N!
function m = computerDifferentPerm(A)
vec = A(:).';
m = perms(vec);
end
Now m is a N! x N Matrix.
Each line is a vector corresponding to one permutation of the original matrix. You now have to reconstruct each one into a 3D matrix.

Shifting specific elements in a logical array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I have this matrix:
0 1 0
1 0 0
0 0 0
0 0 1
0 0 1
0 1 0
0 1 1
I don't know what statement(s) I have to write to change the bottom row only so that it becomes:
0 1 0
1 0 0
0 0 0
0 0 1
0 0 1
0 1 0
1 0 1
Specifically, I'd like to swap the 1st and 2nd column of of the 7th row only.
Not only that but I'd like to write a statement that finds adjacent 1's value in a row such as 1 1 0 or 0 0 0 1 1 0 and then applies the same switching.
Suppose that the variable mat represents your matrix. use:
mat(7,[1,2]) = mat(7,[2,1]);
Where 7 specifies the row which you want to perform the swapping on, and 1 and 2 are the columns to swap.

Random generate unique matrix [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Could you help me. I want to generate matrix of '0' and '1' (for example rows 8 and columns 7; r and c). I want to specify fixed size columns (in this example fixed=3) and on it position in every execution should be all '0'. The rest should be randomly and unique selected. Here is example:
0 0 0 1 0 1 0
0 0 0 0 0 0 1
0 0 0 1 0 0 0
0 0 0 1 1 0 0
0 0 0 0 0 1 1
0 0 0 1 0 1 0
0 0 0 1 1 1 1
0 0 0 0 1 1 1
my_matrix = [zeros(7, 3), floor(rand(7,4)*2)]

make a delay in my vector, matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have vector for example. Ones and zeros are representing traffic and idle states for base station traffic.
u=[1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ]
I know how to calculate number of ones and zeros in it after each break.
But I need help for delay. Zeros are telling me when I can put my base station in sleep mode.
Is there a way to make a delay for example that, base station does not go to sleep after first zero, instead of that that it goes to sleep after third zero, that means with some kind o delay.
Not a simple solution but yet I think you should find it interesting. You can optimize from here.
First of all I assumed you wanted to go to sleep after the third consecutive 0.
Check the example.
CODE:
u=[1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ];
bw_u=bwlabel(u==0);
get_breaks=bsxfun(#eq, bw_u, unique(bw_u)');
pos_break=cumsum(get_breaks,2).*get_breaks;
third_0=pos_break(2:end,:)==3;
[~,indx_third_0]=find(third_0)
OUTPUT:
indx_third_0 =
8
20
32
43
Again assuming ASantosRibeiro interpreted your question correctly, you can do it a little more simply as follows. Let n denote the desired number of zeros. In your case, n=3;
>> ind = find(diff(conv(2*u-1, repmat(-1, [1 n])) == n) == 1) + 1
ind =
8 20 32 43
Assuming ASantosRibeiro interpreted your question correctly, you could do this to find when to "go to sleep":
delay = 3;
idx = find(diff(u)==-1) + delay
idx =
8 20 32 43
This is assuming you want a delay of 3.
diff(u) returns the difference between consecutive elements in u. Finding when the difference equals -1 is equivalent to finding the indices of when the light changes from 1 to 0. By adding the delay, you have your desired indices.

how to generate a matrix of unknown size through for loop in matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The code
conf=ones(103,1);
f=conf;
for k=1:103
f(k:k+1)=1i;
conf=f.*conf;
p(k,:)=conf;
end
Now I actually want to record the result of each iteration in matrix p. So that I can use this product result later in my program.
final p matrix could be like
[i i 1 1 1 1 1...
i -1 i 1 1 1 1.....
i -i -1 i 1 1 1.....
so on].
It looks like you did all the hard fixes already. I think what you want is:
conf=ones(103,1);
f=conf;
for k=1:102 % <--- reduced this because otherwise f(k:k+1) attempts to
% index beyond the size of f
f(k:k+1)=1i;
conf=f.*conf;
p(k,:)=conf;
end
p(103,:)=1i;
I would check the results for a smaller array. For instance if I run the following smaller version of the above (as a test)
conf=ones(5,1);
f=conf;
for k=1:4
f(k:k+1)=1i;
conf=f.*conf;
p(k,:)=conf;
end
p(5,:)=1i;
I get
>> real(p)
ans =
0 0 1 1 1
-1 -1 0 1 1
0 0 -1 0 1
1 1 0 -1 0
0 0 0 0 0
and
>> imag(p)
ans =
1 1 0 0 0
0 0 1 0 0
-1 -1 0 1 0
0 0 -1 0 1
1 1 1 1 1