make a delay in my vector, 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 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.

Related

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)]

Fill incomplete holes of 3D matrix with 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 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.

How to identify the same number in a matrix? [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 7 years ago.
Improve this question
I have a binary matrix, it looks like this:
A = [ 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0;
1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1; ]
But when I try to put A into a calculation, I only can use vector B, which
is the row sum of matrix A into calculation. B looks like this:
B=[ 1 1 0 1 1 1 0 1 1 0 1 1 0 0 1 1 1];
But I still want to carry the information about which "1" comes from which row of matrix A. I want to know is there any way to add additional conditions to vector B, so that vector B still can carry the information from matrix A, that is which "1" comes from which "row" of matrix A.
Assuming that A only contains 0 and 1 values,
[v, B] = max(A,[],1);
B(v==0) = 0;
gives
B =
2 2 0 1 1 1 0 2 2 0 1 1 0 0 2 3 3
If there are more than one 1 value in a column, this gives the row index of the first one.
Its #luis's idea.. i'm just adding little changes. Also i still don't know whether this is what OP wants.
Created a 3D matrix from luis's solution, So that both the binary values and row info are stored in B. If you want binary values, access slice 1. if you want row info, access slice 2
[B(:,:,1), B(:,:,2)] = max(A);
B(1,~all(B,3),:) = 0;
>> B
B(:,:,1) =
1 1 0 1 1 1 0 1 1 0 1 1 0 0 1 1 1
B(:,:,2) =
2 2 0 1 1 1 0 2 2 0 1 1 0 0 2 3 3
If you want a specific binary value and its row index, say for eg, 8th binary value and its corresponding row index,
>> B(:,8,:)
ans(:,:,1) =
1
ans(:,:,2) =
2

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