Finding the position of repeated values in a logical array - find

If I have a vector such as:
0 0 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1 0 1 1 1 1 1 1 0 0 0
How do I find the position of the first time there are two consecutive 1s. I.e. the answer to the above would be 9.
Thanks!

Can't comment, so will give you a hint here: "Finite State Machines"

Related

Using 25 characters or less to create a specific matrix in MATLAB

What matlab command, or combination of commands (using 25 characters or less), could be used to create the following matrix?
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I got as far as this;
repmat(tril(ones(3,3)),5)
But repmat creates a 5 by 5 matrix. I however, need a 4,5 matrix.
Thank you for taking the time to help!
Add one more argument to repmat and remove one from ones (as Divakar noted):
repmat(tril(ones(3)),4,5)
As you can see, you can specify how many replications you want for both the rows and the columns. A single value argument to either function will use that value for both rows and columns.
I'll throw the kron solution out there. Just because.
kron(ones(4,5),tril(ones(3)))
More than 25 characters, but hey:
bsxfun(#le,mod(0:3*5-1,3),mod(0:3*4-1,3).')

distribute matrix values of a binary image

I need to take the tril below and distribute the values equally so it ends up like a modified checkerboard.
x=ones(1186,686);
x2=tril(x);
A sample simplified matrix of what I need to have happen is below:
1 1 1 1 1 1
1 1 1 1 1 1
0 1 1 1 1 1
0 0 1 1 1 1
0 0 0 1 1 1
0 0 0 0 1 1
0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 0
The matrix above needs to be changed into:
1 1 1 1 1 1
1 1 1 1 1 1
1 0 1 1 1 1
1 0 1 0 1 1
1 0 1 0 1 0
1 0 1 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
I've tried looking for built in MATLAB functions that would evenly distribute the zeros values across the board, but have not found anything that works. In terms of the output, where the ones and zeros appear is somewhat irrelevant. They just need to be distributed as evenly as possible within the matrix. For example the 3rd line of the modified matrix could = 1 1 1 0 1 1 or the like.
Would be be possible to achieve this effect a different way than starting with the tril that I'm not seeing?
For Beaker and anyone that wants to comment on how I could have better asked my original question.
This is the result of what I wanted. It's not exactly what I had described, but it achieves the same function I'm after. It's Completely different direction. I'm doing image analysis of the mixing and segregation of particles. Having contrived images like this allows me to determine if my mixing algorithm is producing the results I'm expecting. I can use the interleaving code you gave to continue making new and interesting patterns (not shown).
img=checkerboard(1,1186,686);
img_bw=im2bw(img);
img_mix=triu(img_bw,-500);
img_neg=imcomplement(img_mix);
imshow(img_neg)
I think this might be what you're after. Note that I borrowed the interleave-by-reshape trick from this blog post. (I also used triu instead of tril since it matched your example.)
x=ones(12,10);
x2=triu(x);
[rows,cols]=size(x2);
a = x2(:,1:cols/2);
b = x2(:,end:-1:(cols/2)+1);
% interlave two same sized matrices by column
a = a.';
b = b.';
col_interleave = reshape([b(:) a(:)]',2*size(a,1), []).'
Output is:
col_interleave =
1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 1 1
1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 0 0 0 0
1 0 1 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
0 0 0 0 0 0 0 0 0 0

Matlab how to bitwise-AND of a vector and a matrix?

I want to find out the index of the matrix column in which the vector appears. My idea is to do AND of the vector over matrix and only the column that is the same will be 1 in the new vecotr. But I don't know how to do this. Below is example:
H =
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0
0 1 1 1 0 0 0 1 1 1 1 0 1 0 0
1 0 1 1 0 1 1 0 0 1 1 0 0 1 0
1 1 0 1 1 0 1 0 1 0 1 0 0 0 1
S =
0 1 0 1
From that I want to get 2 as second column or even better vector
0 1 0 0 0 0 ... 0
Since there is error in second column.
How can I do this in Matlab or even better Octave?
Not really sure how you tried to approach the problem. But with repmat or bsxfun it is as simple as this:
all(bsxfun(#eq,H,S'))
How about
result = sum(H==repmat(S(:),[1 size(H,2)]))==4;
I found out the function
ismember(H', S, "rows")
works exactly as I want. Your answers are all good too, thanks.
That's pretty easy with broadcasting. The following will require Octave 3.6.0 or later but you can use bsxfun if you have a previous version:
octave-cli-3.8.1> h = logical ([
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0
0 1 1 1 0 0 0 1 1 1 1 0 1 0 0
1 0 1 1 0 1 1 0 0 1 1 0 0 1 0
1 1 0 1 1 0 1 0 1 0 1 0 0 0 1]);
octave-cli-3.8.1> s = logical ([0 1 0 1]');
octave-cli-3.8.1> all (h == s)
ans =
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
From here, it's all a matter of using find to get the column numbers. It will even work if it matches more than 1 column:
octave-cli-3.8.1> find (all (h == s))
ans = 2

Logical operations on matrices columns.

Let say that I have 1 matrix with numbers (0,1). How can i create new matrix that is the result of a logical operation among the columns?
eg. A =
0 0 0 1 0
1 1 1 1 1
0 1 1 0 0
0 0 0 0 1
1 0 0 1 0
1 1 1 1 1
If all elements of **rows** are equal to 1 - 1, if not - 0.
(like AND operation)
Ans= 0
1
0
0
0
1
Thanks!
To solve your problem this would work -
all(A,2)
If you were looking to set elements based on the columnwise data in A, you would do this -
all(A,1)
More info on all, must serve you well.

Centroid of objects/connected components in matlab?

I am trying to find centroid of objects. I have already implemented connected components labeling and I have developed following code for centroid, it does give result but does not gives correct result:
I have following output matrix i.e matrix_img:
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 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 0 0 0 0 1 1 1 1 0 0
2 2 2 2 0 0 0 0 0 1 1 1 1 1 1 0
2 2 2 2 0 0 0 0 1 1 1 1 1 1 1 1
2 2 2 2 0 0 0 0 1 1 1 1 1 1 1 1
2 2 2 2 0 0 0 0 1 1 1 1 1 1 1 1
2 2 2 2 0 0 0 0 0 1 1 1 1 1 1 0
2 2 2 2 0 0 0 0 0 0 1 1 1 1 0 0
2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 2 0 0 5 5 5 0 0 0 0 0 0 0
2 2 2 2 0 0 5 5 5 0 0 0 0 0 0 0
2 2 2 2 0 0 5 5 5 0 0 0 0 0 0 0
2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0
and following is the code
n= max(max(matrix_img));
for k=1:n
a(k)=length(find(matrix_img==k));
sx(k)=0;
sy(k)=0;
cx=0;
cy=0;
for i=1:1:r
for j=1:1:c
if(matrix_img(i,j)==k)
sx(k)=sx(k)+i;
sy(k)=sy(k)+j;
cx=sx(k)/a(k);
cy=sy(k)/a(k);
end
end
end
fprintf('Centroid of Object %d is %d and %d \n', k, cx, cy);
end
It gives result like :
Centroid of Object 1 is 7 and 1.250000e+001
Centroid of Object 2 is 1.050000e+001 and 2.500000e+000
Centroid of Object 3 is 0 and 0
Centroid of Object 4 is 0 and 0
Centroid of Object 5 is 14 and 8
Object 5 result is correct, object 2 is completely wrong and object 1 is partially wrong.. what shall I do?
The values you obtain are the exact centroids for those objects. So you might want to define what you'd expect to get as results.
To make this more clear, I've colored the objects in your matrix. By the symmetry on your cartesian grid, there should be an equal number of points to the left and right of your centroid and the same for above/below the centroid. I've drawn a figure with your objects colored in, together with lines to represent the horizontal and vertical center lines. Those are lines for which, we have an equal number of points to the left/right (or above/below) of them that are part of a certain object.
Their intersection is the centroid, so you can see for object 5 (blue one) that the centroid is at (8, 14). For the other two objects, these center lines do not lie on the integer grid you have: the red object (1) has its centroid at (12.5, 7) which is also the outcome of your code and the green object (2) is centered around (2.5, 10.5).
You will either have to live with inaccuracy introduced by rounding your centroids (e.g. round(cx)) or you will have to live with the non-integer coordinates of the centroids.
Next to that, I also recommend you vectorize your code as oli showed: this allows you to run your code faster and it is easier to understand when you are somewhat familiar with MATLAB than for loops.
Perhaps a little note with regard to your string representation: don't use %d for non-integers, as you see that will cause real numbers to be displayed in scientific notation. I think it's clearer if you use something like %0.2f as your format string.
When you use matlab, avoid using loops, it makes your code very slow, and it is much longer.
You can do the same thing by doing that:
[y x]=ndgrid(1:size(matrix_img,1),1:size(matrix_img,2));
n=max(matrix_img(:));
for k=1:n
cy=mean(y(matrix_img==k));
cx=mean(x(matrix_img==k));
fprintf('Centroid of Object %d is %2.2g and %2.2g \n', k, cx, cy);
end
(Maybe you want to swap x and y in that code)