Random generate unique matrix [closed] - matlab

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

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.

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 get the posiible combinations using 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
How can i get the possible combinations to get 20 'i's from the given vector:
dn = 0 i i i i i i i i i i i i 0 i 0 i i i i i i i 0 i i i i 0 i i i i i i i i i 0 0 i i i i i i i 0 i i i 0 i i 0 i i i 0 i 0 0 0 0 0 0 i i 0 0 0 0 0 0 i i 0 0 0 0 i i 0 0 0 i 0 i 0 0 i 0 i 0 i i 0 i i 0
My objective
1. No of possible combinations with each combination having 20 i
2. Index value of each 'i's for all combination
Example:
var = 0 i i 0 i 0 i i 0 0 0 i
Here I need posiible combinations with 2 'i's
I can form the combinations like (2,3),(2,5),(3,5),(2,7) and so on.
I think this does what you want:
var = [0 i i 0 i 0 i i 0 0 0 i];
N = 2;
result = nchoosek(find(var==i), N);
In your example, this gives
result =
2 3
2 5
2 7
2 8
2 12
3 5
3 7
3 8
3 12
5 7
5 8
5 12
7 8
7 12
8 12

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