a command to view result on matlab for the tarjan algorithm - matlab

I'm working on an implementation of the Tarjan algorithm in Matlab.
I use this source code to determine the strongly connected components.
This is the result I got, how can I view the result with Matlab (a figure that determines the colored strongly connected components)?
What is the appropriate command?
G=[0 0 1 1 0 0 0;
1 0 0 0 0 0 0;
0 0 0 0 0 1 0;
0 0 0 0 1 0 0;
0 0 0 0 0 0 1;
0 0 0 1 0 0 0;
0 0 0 0 0 1 0];
tarjan(G)
ans =
7 5 4 6 0 0 0
3 0 0 0 0 0 0
1 0 0 0 0 0 0
2 0 0 0 0 0 0

There is already an examle using coloring, all nodes listed in the first row are colored with the first color, all nodes listed in the second row are colored with the second color etc...

Related

How to correctly use the col2im function?

This function is confusing to use, and it always gives me an error:
To RESHAPE the number of elements must not change.
That's my code:
im=im2col(zeros(300,300),[3 3]);
im(:,9)=ones(9,1);
im=col2im(im,[3 3],[300 300]);
Basically, this code just gets the block at index 6, replaces it with ones, and reassembles it back into the original image. What's wrong with this code?
It seems you want to create distinct blocks from your input array, change single blocks, and rearrange them. (Your target size is the same as your input array size.) So, you must use the distinct parameter in both, im2col as well as col2im:
blk_size = [3, 3];
im = zeros(9, 9)
temp = im2col(im, blk_size, 'distinct');
temp(:, 3) = ones(prod(blk_size), 1);
im2 = col2im(temp, [3 3], size(im), 'distinct')
Output:
im =
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 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
im2 =
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 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
When using im2col with the sliding parameter, which is also the default, if no parameter is set at all, there'll be a lot more columns in the result than can be rearranged to the input array size, cf. the Tips section on im2col.
Hope that helps!

Equivalent of an Octave command in MATLAB [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Matlab Error: ()-indexing must appear last in an index expression
(1 answer)
Closed 5 years ago.
I'm trying to realise the following Octave command in MATLAB:
M = eye(x)(y,:);
x is just a number (in my example 10) and y is a vector (here 8x1):
y = [1 3 4 5 7 10 9 10];
The Octave command would generate:
M =
1 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
The ones are kept very near to the diagonal.
The nearest I came with MATLAB is with the following commands:
n = size(y,1);
Y = eye(n, x);
but it would generate something still different. If the difference between rows and columns gets bigger, it would be very different.
M =
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
How could I get the first matrix with MATLAB?
First you should find what this expression eye(x)(y,:) means. First create an identity matrix with the size of x by x, and then select rows with index in y. Therefore, the equivalent syntax would be:
E = eye(x);
M = E(y,:);

Extract multiple indices from matrix

I want to access multiple indices of a matrix as shown below. So what I want is indices (1,3),(2,6),(3,7) to be set to one. However, as you can see the entire column is set to one. I can see what it is doing but is there a way to do what I want it to (in an elegant way - no loops).
a=zeros(3,10)
a(1:3,[3 6 7])=1
a =
0 0 1 0 0 1 1 0 0 0
0 0 1 0 0 1 1 0 0 0
0 0 1 0 0 1 1 0 0 0
I realise that you can do something along the lines of
x_idx=1:3, y_idx=[3 6 7];
idx=x_idx*size(a,2)+y_idx;
a(idx)=1;
but just wondering if there was a better, or proper way of doing this in Matlab
You can use sub2ind, which essentially is doing what you have mentioned in your post, but MATLAB has this built-in:
a = zeros(3,10);
a(sub2ind(size(a), 1:3, [3 6 7])) = 1
a =
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
Another way would be to create a logical sparse matrix, then use this to index into a:
a = zeros(3,10);
ind = logical(sparse(1:3, [3 6 7], true, size(a,1), size(a,2)));
a(ind) = 1
a =
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0

matlab's imfill function seems bugged?

I need to work with imfill in Matlab (Version 2010b, 7.11.0). I now think there is a bug in the program.
The most simple example that i found here is following: (Fills the Image background (0) beginning at the position [4 3])
BW = [ 0 0 0 0 0 0 0 0;
0 1 1 1 1 1 0 0;
0 1 0 0 0 1 0 0;
0 1 0 0 0 1 0 0;
0 1 0 0 0 1 0 0;
0 1 1 1 1 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0];
imfill(BW,[4 3])
According to the specifications this should work IMHO, but I always get following message. Can anyone tell me what I am doing wrong?
??? Error using ==> iptcheckconn at 56
Function IMFILL expected its second input argument, CONN,
to be a valid connectivity specifier.
A nonscalar connectivity specifier must be 3-by-3-by- ...
-by-3.
Error in ==> imfill>parse_inputs at 259
iptcheckconn(conn, mfilename, 'CONN', conn_position);
Error in ==> imfill at 124
[I,locations,conn,do_fillholes] = parse_inputs(varargin{:});
Error in ==> test at 9
imfill(BW,[4 3])
That does not explain the problem but converting BW to a logical array does work. I'm not sure as to why it's like this though:
clear
close all
clc
BW = [ 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0
0 1 0 0 0 1 0 0
0 1 0 0 0 1 0 0
0 1 0 0 0 1 0 0
0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
BW2 = imfill(logical(BW),[4 3])
BW2 =
0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
As you have already seen in the other solution by #Benoit_11, that most probably that input wasn't of logical class, which was throwing an error at you. So, you are set there!
Now, I would like to put forth a tiny bit of bonus suggestion here.
Let's suppose you have a set of seed points with their row and column IDs and you would like to fill an image with those seed points in one go. For that case,
you need to use those IDs as column vectors. Thus, if you have the row and column IDs as -
row_id = [4 3];
col_id = [3 7];
You can fill image with this -
BW = imfill(BW,[row_id(:) col_id(:)])
But, the following code would throw error at you -
BW = imfill(BW,[row_id col_id])

Find out first circle in an image on Matlab (wrt. y-axis)

I'm using Matlab. I have a 2-D Binary image/array. like this
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0 1 0 0
0 0 1 1 0 0 1 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 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
I want to find out center of very first white block/Circle with respect to y-axis
Answer of the above image will be.
0 1 0
1 1 1
0 1 0
Anyone who have have a simplest solution for this.
If you are looking for exact matches of the template, you can use a moving filter, one example is:
H=[0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 1 1 1 0 0 1 0 0;
0 0 1 1 0 0 1 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 1 0 0 0 0 0 0;
0 0 0 0 0 0 1 1 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0];
b=[0 1 0;
1 1 1;
0 1 0];
C=filter2(b,H, 'same');
[x,y]=find(C==max(max(C)));
x and y are the locations of your template in the order that it appears from the top left corner of your array.
Edit: if you have the Image Processing Toolbox and are looking for a less strict way of finding objects that have a roughly circular shape you can use regionprops with the 'Centroid' and 'Eccentricity' arguments with the bwconncomp function.
ObjectStats=regionprops(bwconncomp(H,4), 'Centroid', 'Eccentricity');
Objects with an 'Eccentricity' of 0 (or close to 0) will be the circles.
idx=find(cell2mat({ObjectStats.Eccentricity})==0); % Change ==0 to <0.2 or something to make it less strict.
ctrs={ObjectStats.Centroid};
>> ctrs{1,idx(1)}
ans =
7 3
Note that in your case, a lone pixel is an object with an eccentricity of 0, it is the smallest 'circle' that you can find. If you need to define a minimum size, use the 'Area' property of regionprops
You can do this with a simple 2 dimensional convolution. It will "overlay" the filter along a larger matrix and multiply the filter by the values it is overlaying. If the product is equal to the sum of the filter, then you know you found a match.
Here is some simple code.
mat = [0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0 1 0 0
0 0 1 1 0 0 1 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 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0];
filt = [0 1 0
1 1 1
0 1 0];
[row,col] = find(conv2(mat,filt,'same') == sum(filt(:)))