SeDuMi Matlab Code for Optimization problem - matlab

have this optimization Problem and need to implement this problem using SeDuMi Technique in MATLAB. Here, Q_i = h_i * h_i^(H). and h_i are randomly generated vector. I would like someone to help me with the matlab code for the implementation of this optimization problem in SeDuMi:
Matlab code, which i have been working on:
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %
clear all; clc
N = 10; % Number of antennas # BS
M = 5; % Number of single antenna User
H = (randn(N,M) + 1j*randn(N,M)) *sqrt(0.5);
Q = zeros(N,N,M);
vecQs= [];
for k = 1:M
Q(:,:,k) =H(:,k)'*H(:,k);
vecQs = [vecQs,vec(Q(k).')];
end
A=[-eye(M),vecQs.'];
b=ones(M,1);
c=[zeros(M,1);vec(eye(N))];
K.l=M;
K.s=N;
K.scomplex=1;
[x_opt,y_opt,info]=sedumi(A,b,c,K);
Xopt=mat(x_opt(M+1:end));

Related

Failing to use interpolation instead of integration in chuncks with ODE45

I have written this code to integrate voltage V and gating conductances m, n and h in 30ms with injection of current from ms 10 to 20. So basically Current I is a vector that has 0 values until 10ms, 1 values until 20ms and 0 values until 30ms. The code works well and it shows the behaviour I want to see.
I'm trying to implement the same code with interpolation instead of integration in chunks. However, I'm failing to do so.
This is the integration with interpolation, which is giving the incorrect result:
myode = #(T,y0) [((1/Cm)*interp1(t, I, T, 'linear','extrap')-(INa+IK+Il)); % Normal case
alpham(y0(1,1))*(1-y0(2,1))-betam(y0(1,1))*y0(2,1);
alphan(y0(1,1))*(1-y0(3,1))-betan(y0(1,1))*y0(3,1);
alphah(y0(1,1))*(1-y0(4,1))-betah(y0(1,1))*y0(4,1)];
[time,V] = ode45(myode, t, y0, I);
This is the correct one, in chunks:
[time,V] = ode45(#ODEMAT, [t(chunk), t(chunk+1)], y);
%%% Calls ODEMAT, in which theres' the integration (extensive code posted below):
dydt = [((1/Cm)*(I(chunk)-(INa+IK+Il))); % Normal case
alpham(V)*(1-m)-betam(V)*m;
alphan(V)*(1-n)-betan(V)*n;
alphah(V)*(1-h)-betah(V)*h];
This is the code with the integration in chunks:
function Z2_chunks ()
%% Initial values
V=-60; % Initial Membrane voltage
m1=alpham(V)/(alpham(V)+betam(V)); % Initial m-value
n1=alphan(V)/(alphan(V)+betan(V)); % Initial n-value
h1=alphah(V)/(alphah(V)+betah(V)); % Initial h-value
y0=[V;m1;n1;h1];
t = [1:30];
I = [zeros(1,10),ones(1,10),zeros(1,10)];
% Plotting purposes (set I(idx) equal to last value of I)
idx = numel(t);
I(idx) = 0.1;
chunks = numel(t) - 1;
for chunk = 1:chunks
if chunk == 1
V=-60; % Initial Membrane voltage
m=alpham(V)/(alpham(V)+betam(V)); % Initial m-value
n=alphan(V)/(alphan(V)+betan(V)); % Initial n-value
h=alphah(V)/(alphah(V)+betah(V)); % Initial h-value
y=[V;m;n;h];
else
y = V(end, :); % Final position is initial value for next interval
end
[time,V] = ode45(#ODEMAT, [t(chunk), t(chunk+1)], y);
if chunk == 1
def_time = time;
def_v = V;
else
def_time = [def_time; time];
def_v = [def_v; V];
end
end
OD = def_v(:,1);
ODm = def_v(:,2);
ODn = def_v(:,3);
ODh = def_v(:,4);
time = def_time;
%% Plots
%% Voltage
figure
subplot(3,1,1)
plot(time,OD);
legend('ODE45 solver');
xlabel('Time (ms)');
ylabel('Voltage (mV)');
title('Voltage Change for Hodgkin-Huxley Model');
%% Current
subplot(3,1,2)
stairs(t,I)
ylim([0 5*max(I)])
legend('Current injected')
xlabel('Time (ms)')
ylabel('Ampere')
title('Current')
%% Gating variables
subplot(3,1,3)
plot(time,[ODm,ODn,ODh]);
legend('ODm','ODn','ODh');
xlabel('Time (ms)')
ylabel('Value')
title('Gating variables')
function [dydt] = ODEMAT(t,y)
%% Constants
ENa=55; % mv Na reversal potential
EK=-72; % mv K reversal potential
El=-49; % mv Leakage reversal potential
%% Values of conductances
gbarl=0.003; % mS/cm^2 Leakage conductance
gbarNa=1.2; % mS/cm^2 Na conductance
gbarK=0.36; % mS/cm^2 K conductancence
Cm = 0.01; % Capacitance
% Values set to equal input values
V = y(1);
m = y(2);
n = y(3);
h = y(4);
gNa = gbarNa*m^3*h;
gK = gbarK*n^4;
gL = gbarl;
INa=gNa*(V-ENa);
IK=gK*(V-EK);
Il=gL*(V-El);
dydt = [((1/Cm)*(I(chunk)-(INa+IK+Il))); % Normal case
alpham(V)*(1-m)-betam(V)*m;
alphan(V)*(1-n)-betan(V)*n;
alphah(V)*(1-h)-betah(V)*h];
end
end
This is the code with the integration with interpolation. As you can see, the plots are really different:
function Z1_interpol ()
%% Initial values
V=-60; % Initial Membrane voltage
m1=alpham(V)/(alpham(V)+betam(V)); % Initial m-value
n1=alphan(V)/(alphan(V)+betan(V)); % Initial n-value
h1=alphah(V)/(alphah(V)+betah(V)); % Initial h-value
y0=[V;m1;n1;h1];
t = [1:30];
I = [zeros(1,10),ones(1,10),zeros(1,10)];
% Plotting purposes (set I(idx) equal to last value of I)
idx = numel(t);
I(idx) = 0.1;
V=-60; % Initial Membrane voltage
m=alpham(V)/(alpham(V)+betam(V)); % Initial m-value
n=alphan(V)/(alphan(V)+betan(V)); % Initial n-value
h=alphah(V)/(alphah(V)+betah(V)); % Initial h-value
y=[V;m;n;h];
%% Constants
ENa=55; % mv Na reversal potential
EK=-72; % mv K reversal potential
El=-49; % mv Leakage reversal potential
%% Values of conductances
gbarl=0.003; % mS/cm^2 Leakage conductance
gbarNa=1.2; % mS/cm^2 Na conductance
gbarK=0.36; % mS/cm^2 K conductancence
Cm = 0.01; % Capacitance
%% Initial values
V=-60; % Initial Membrane voltage
m=alpham(V)/(alpham(V)+betam(V)); % Initial m-value
n=alphan(V)/(alphan(V)+betan(V)); % Initial n-value
h=alphah(V)/(alphah(V)+betah(V)); % Initial h-value
y0=[V;m;n;h];
gNa = gbarNa*m^3*h;
gK = gbarK*n^4;
gL = gbarl;
INa=gNa*(V-ENa);
IK=gK*(V-EK);
Il=gL*(V-El);
myode = #(T,y0) [((1/Cm)*interp1(t, I, T, 'linear','extrap')-(INa+IK+Il)); % Normal case
alpham(y0(1,1))*(1-y0(2,1))-betam(y0(1,1))*y0(2,1);
alphan(y0(1,1))*(1-y0(3,1))-betan(y0(1,1))*y0(3,1);
alphah(y0(1,1))*(1-y0(4,1))-betah(y0(1,1))*y0(4,1)];
[time,V] = ode45(myode, t, y0, I);
OD=V(:,1);
ODm=V(:,2);
ODn=V(:,3);
ODh=V(:,4);
time = time;
%% Plots
%% Voltage
figure
subplot(3,1,1)
plot(time,OD);
legend('ODE45 solver');
xlabel('Time (ms)');
ylabel('Voltage (mV)');
title('Voltage Change for Hodgkin-Huxley Model');
%% Current
subplot(3,1,2)
stairs(t,I)
ylim([0 5*max(I)])
legend('Current injected')
xlabel('Time (ms)')
ylabel('Ampere')
title('Current')
%% Gating variables
subplot(3,1,3)
plot(time,[ODm,ODn,ODh]);
legend('ODm','ODn','ODh');
xlabel('Time (ms)')
ylabel('Value')
title('Gating variables')
end

Matlab: use plot and semilogy in same graph (Matlab 2014)

I want to use plot and semilog in the same graph so as to compare results. I get only the one waveform though using hold on function. What can I do ? I cant use yyaxis left cause my matlab is lower than 2016. Any help?
Here is my code:
figure
semilogy(VG, ID3)
hold on
plot(VG, ID3)
hold off
A possible solution to have both the linear and the semilog chart in the same graph could be to create two overlapping graphs:
on the first you can plot the data with the linear scale
on the second you can plot the data with the logarithmic scale either on the x or axis
The main steps are:
create a figure
create the first axes in the figure
set the color of the axes to the same color used for the data, this will help recognising the data
plot the data with plot
create the second axes in the figure and set its size equal to the one of the first axes (now the secon axes is the one "active")
move the X axis location to the top of the chart
move the Yaxis location to the right of the chart
plot the data with semilogy
set the color of the axes to the same color used for the data, this will help recognising the data
add the title, the legend and the x / y labels
Since we have changed the location of the X and Y axis of the second axes, we need to adjust the dimenisons of the figure and of the axes so thay fit toghter
Now you can fix the problem of the different grid of the two axes:
to do that, you can add a menu item to the menu bar (with the uimenu function) to toggle the grid
In the following, a possible implementation of the suggested approach.
Since you have not specified if you use R2014a or R014b, in the code below you can find both the way to set the properties of the figure and axes:
the "old" way: using get / set
the "new" one dot notation available from R2014b
(the last one is "commented")
% Define input data
x=linspace(0,30,30)
y=rand(30, 1);
% Cretate a Figure
f=figure('units','normalized')
% Create the first axes in the figure
ax1=axes
% Plot the data with "PLOT"
ph=plot(x,y,'r')
% Set the axis labeks
xlabel('X data')
ylabel('Y data, LIN mode')
% Get the position of the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1_pos=ax1.Position
ax1_pos=get(ax1,'position')
% Set the color of the fist axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.XColor='r'
% % % % % % % % % % % % % % % % % % % % % % % % ax1.YColor='r'
set(ax1,'xcolor','r','ycolor','r')
% Add the second axes in the figure
ax2=axes('position',ax1_pos)
% Plot the data with SEMILOGY
slh=semilogy(x,y,'b')
% Set the ylabel
ylabel('Y data, LOG mode')
% Set the title of the chrt
title('Linear and Semilog Plot')
% Move the X axis location to the top of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.XAxisLocation='top'
set(ax2,'XAxisLocation','top')
% Move the XYaxis location to the right of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.YAxisLocation='right'
set(ax2,'YAxisLocation','right')
% Set the color of the second axes to transparent
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.Color='none'
set(ax2,'color','none')
% Set the color of the second axes
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.XColor='b'
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.YColor='b'
set(ax2,'xcolor','b','ycolor','b')
% Add the lgend to the chart
legend([ph slh],'plot','semilogy','location','best')
% Adjust the size of the the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.Position=[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9]
set(ax1,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% et the size of the second axes as per the first one
ax2.Position=ax1.Position
set(ax2,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% Adjust the size of the figure
% % % % % % % % % % % % % % % % % % % % % % % % % % % fp=f.Position
fp=get(f,'position')
% % % % % % % % % % % % % % % % % % % % % % % % % % % f.Position=[0.1,0.1,fp(3)*1.2,fp(4)*1.2]
set(f,'position',[0.1,0.1,fp(3)*1.2,fp(4)*1.2])
% Add the menu to manage the grid
mh=uimenu('Label','Grid manag.')
m1h=uimenu(mh,'Label','Linear Grid','callback', ...
'axes(ax1);grid;axes(ax2);if(strcmp(m1h.Checked,''off'')),m1h.Checked=''on'';else,m1h.Checked=''off'';end')
m2h=uimenu(mh,'Label','Log Grid','callback', ...
'axes(ax2);grid;if(strcmp(m2h.Checked,''off'')),m2h.Checked=''on'';else,m2h.Checked=''off'';end')
I often have to make figures comparing data in linear and log space. Stacking the plots using subplot() gives a nice visual. Example:
% set up dummy data that is evenly-space in logspace
x = 10.^((linspace(log10(.01), log10(10),20))');
y = rand(20, 1);
% finish setup
figure
subplot(2,1,1)
plot(x, y, 'DisplayName', 'MyData')
title('Plot with Linear Axes')
xlabel('X-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')
subplot(2,1,2)
semilogx(x, y, 'DisplayName', 'MyData')
title('Plot with LogX Axis')
xlabel('LogX-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')
The above produces this:

bin2mat running slow need a faster solution

I am using bin2mat function from matlab file exchange, for some reason it runs very slow. Is it possible to make it run faster or is there an alternative? I am trying: zC = bin2mat(s.X,s.Y,s.Z,xC,yC); I am not sure where it gets bogged down. I need to do this on point cloud data to calculate volume and such.
Here is the code:
function ZG = bin2mat(x,y,z,XI,YI,varargin)
% BIN2MAT - create a matrix from scattered data without interpolation
%
% ZG = BIN2MAT(X,Y,Z,XI,YI) - creates a grid from the data
% in the (usually) nonuniformily-spaced vectors (x,y,z)
% using grid-cell averaging (no interpolation). The grid
% dimensions are specified by the uniformily spaced vectors
% XI and YI (as produced by meshgrid).
%
% ZG = BIN2MAT(...,#FUN) - evaluates the function FUN for each
% cell in the specified grid (rather than using the default
% function, mean). If the function FUN returns non-scalar output,
% the output ZG will be a cell array.
%
% ZG = BIN2MAT(...,#FUN,ARG1,ARG2,...) provides aditional
% arguments which are passed to the function FUN.
%
% EXAMPLE
%
% %generate some scattered data
% [x,y,z]=peaks(150);
% ind=(rand(size(x))>0.9);
% xs=x(ind); ys=y(ind); zs=z(ind);
%
% %create a grid, use lower resolution if
% %no gaps are desired
% xi=min(xs):0.25:max(xs);
% yi=min(ys):0.25:max(ys);
% [XI,YI]=meshgrid(xi,yi);
%
% %calculate the mean and standard deviation
% %for each grid-cell using bin2mat
% Zm=bin2mat(xs,ys,zs,XI,YI); %mean
% Zs=bin2mat(xs,ys,zs,XI,YI,#std); %std
%
% %plot the results
% figure
% subplot(1,3,1);
% scatter(xs,ys,10,zs,'filled')
% axis image
% title('Scatter Data')
%
% subplot(1,3,2);
% pcolor(XI,YI,Zm)
% shading flat
% axis image
% title('Grid-cell Average')
%
% subplot(1,3,3);
% pcolor(XI,YI,Zs)
% shading flat
% axis image
% title('Grid-cell Std. Dev.')
%
% SEE also RESHAPE ACCUMARRAY FEVAL
% A. Stevens 3/10/2009
% astevens#usgs.gov
%check inputs
error(nargchk(5,inf,nargin,'struct'));
%make sure the vectors are column vectors
x = x(:);
y = y(:);
z = z(:);
if all(any(diff(cellfun(#length,{x,y,z}))));
error('Inputs x, y, and z must be the same size');
end
%process optional input
fun=#mean;
test=1;
if ~isempty(varargin)
fun=varargin{1};
if ~isa(fun,'function_handle');
fun=str2func(fun);
end
%test the function for non-scalar output
test = feval(fun,rand(5,1),varargin{2:end});
end
%grid nodes
xi=XI(1,:);
yi=YI(:,1);
[m,n]=size(XI);
%limit values to those within the specified grid
xmin=min(xi);
xmax=max(xi);
ymin=min(yi);
ymax=max(yi);
gind =(x>=xmin & x<=xmax & ...
y>=ymin & y<=ymax);
%find the indices for each x and y in the grid
[junk,xind] = histc(x(gind),xi);
[junk,yind] = histc(y(gind),yi);
%break the data into a cell for each grid node
blc_ind=accumarray([yind xind],z(gind),[m n],#(x){x},{NaN});
%evaluate the data in each grid using FUN
if numel(test)>1
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind,'uni',0);
else
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind);
end
It is slower on these two steps for one run it took:
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind); took 33 secs
blc_ind=accumarray([yind xind],z(gind),[m n],#(x){x},{NaN}); took 10 secs
You can change blc_ind = ... to
ZG=accumarray([yind xind],z(gind),[m n],#mean,NaN);
and delete other codes form here so the is no need to if numel(test)>1....

Reverse 'buffer' function in Matlab

MATLAB's buffer function partitions a vector into a matrix where each column is a segment of the vector (time series in my problem). These segments can be overlapping, and the overlap does not need to be 50%.
I was wondering if there is a reverse operation where one would get back a vector after doing some operations on the matrix? I was thinking of a generic solution where the overlap is not 50%.
I have searched the question archive and couldn't find any answer.
Thanks
You can use this simple function I wrote. There is also a simple example commented that you can run and test.
function invbuff = invbuffer(X_buff0, noverlap, L)
% Example:
% % % A = (1:40)';
% % % N_over = 2;
% % % N_window = 15;
% % % L = length(A);
% % % Abuff0 = buffer(A, N_window, N_over);
% % % Abuff = Abuff0(:, 1:end-0);
% % % invbuff = invbuffer(Abuff, N_over, L);
invbuff0 = [];
for jj=1:size(X_buff0,2)
vec00 = X_buff0(:,jj);
vec00(1:noverlap) = []; % remove overlapping (or it is zero padding of first frame)
invbuff0 = [invbuff0; vec00];
end
invbuff = invbuff0;
invbuff(L+1:end) = []; % remove zero padding of last frame
% sum(sum([A - invbuff])); % == 0
end
Good luck!

index out of bounds in matlab [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am not able to get how to run it, can anyone help me on this please.
I am receiving this error :
K>> [minutiae_valid]=postprocess_TICO(B,im,~K)
??? Attempted to access wind(273,386); index out of bounds because size(wind)=[368,385].
Error in ==> postprocess_TICO at 214
if (thinned(p,q)==1)&&(wind(p,q)==0)
postprocess_Tico
function [minutiae_valid]=postprocess_TICO(minutiae_valid,im1,thinned)
% clc;
% close all;
% clear all;
cell=16;%total no. of cells needed in quantization;
% im1= imread('E:\FINGERPRINT DATABASE\FVC2002\Dbs\Db1_a\16_6.tif');
wl=(33-1)/2;
% no=20;
% no2=20;
% nox = 0;
% noy = 0; %to choose border area
%
m=size(im1,1);
n=size(im1,2);
%m=(round(m/cell))*cell;
%n=(round(n/cell))*cell;
% im1=imresize(im1,[m n]);
% im=double(im1);
% figure;
% subplot(1,2,1);
% imshow(uint8(im));
% title('original input image');
%
% %Identify ridge-like regions and normalise image--------------------------
% blksze = 10;
% thresh =30;
% M0=100;
% STD0=150;
% [thinim, mask] = imgenhance(im, blksze, M0, STD0);
% subplot(1,2,2); subimage((thinim));
% title('Improved thinned input image', 'FontSize', 12);
% INPUT_IMG=thinim;
%
% %minutiae extraction
% thinned= INPUT_IMG;
% img = INPUT_IMG;
% [minutiae, minutiae_img, combined] = findminutiae(thinned, img);
% totminu_I = size(minutiae,1)
% figure,
% subplot(1,2,1), subimage(combined), title('minutiae before postprocess.')
% %______________________________________________________________
% %TO FIND BOUNDARY OF INPUT IMAGE AS WELL AS REGION OF FALSE MINU
% mask_input=mask;
% border=uint8(zeros(m,n));
%
% [m,n]=size(mask);
% validr=uint8(zeros(m,n));
% thin_temp=thinned;
% for i=1:m
% for j=2:n-1
% if((mask_input(i,j-1)==0)&&(mask_input(i,j)==1)&&(j<=n-no))
% border(i,j)=1;
% validr(i,j-nox:j+no)=1;
% thin_temp(i,j-nox:j+no)=1;
% end
%
% if ((mask_input(i,j+1)==0)&&(mask_input(i,j)==1)&&(j>no))
% border(i,j)=1;
% validr(i,j+nox:-1:j-no)=1;
% thin_temp(i,j+nox:-1:j-no)=1;
% end
%
% end
% end
%
% for i=1:m
% for j=1:n-1:n
% if (mask_input(i,j)==1)
% if (j==1)
% border(i,j)=1;
% validr(i,j:j+no)=1;
% thin_temp(i,j:j+no)=1;
% end
% if (j==n)
% border(i,j)=1;
% validr(i,j:-1:j-no)=1;
% thin_temp(i,j:-1:j-no)=1;
% end
% end
% end
% end
%
%
%
% for j=1:n
% for i=2:m-1
% if((mask_input(i-1,j)==0)&&(mask_input(i,j)==1)&&(i<=m-no))
% border(i,j)=1;
% validr(i-noy:i+no,j)=1;
% thin_temp(i-noy:i+no,j)=1;
% end
% if((mask_input(i+1,j)==0)&&(mask_input(i,j)==1)&&(i>no))
% border(i,j)=1;
% validr(i+noy:-1:i-no,j)=1;
% thin_temp(i+noy:-1:i-no,j)=1;
% end
%
% end
% end
%
% for j=1:n
% for i=1:m-1:m
% if (mask_input(i,j)==1)
% if (i==1)
% border(i,j)=1;
% validr(i:i+no,j)=1;
% thin_temp(i:i+no,j)=1;
% else validr(i:-1:i-no,j)=1;
% thin_temp(i:-1:i-no,j)=1;
% end
% end
% end
% end
%
% %___________________________________________________________________
% % removing minu at the border of roi
% for x=1:size(thinned,1)
% for y=1:size(thinned,2)
% if ((validr(x,y)==1)&&(thinned(x,y)==1))
% combined(x,y,:)=[255,255,255];
% minutiae_img(x,y,:)=[0,0,0];
%
% end
% end
% end
%
% for i=1:totminu_I
% x=minutiae(i,1);
% y=minutiae(i,2);
% if ((validr(x,y)==1)&&(thinned(x,y)==1))
% minutiae(i,:)=0;
% end
% end
% %__________________________
%
% subplot(1,2,2), subimage(combined), title('after boundary effect ')
% minu_count=1;
% minutiae_valid(minu_count, :) = [0,0,0,0]; %to make x, y, CN, theta value of minutiae not in border region.
% for i=1:totminu_I
% CN=minutiae(i,3);
% if (CN~=0)
% minutiae_valid(minu_count, :) = minutiae(i,:);
% minu_count = minu_count + 1;
% end
% end
% totminu_I= minu_count-1;
k=size(minutiae_valid);
totminu_I=k(1,1);
%FALSE RIDGE BIFURCATION
for i=1:totminu_I
if minutiae_valid(i,3)==3
wind=ones(m,n);
xi= minutiae_valid(i,1);
yi=minutiae_valid(i,2);
x=xi;
y=yi;
wind(xi-wl:xi+wl,yi-wl:yi+wl)=0; %window of size 25 cross 25
wind(xi,yi)=-1;
%mark '1', '2', '3'.
value=0;
r=x-1;
for c=y-1:y+1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
c=y+1;
for r=x-1:x+1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
r=x+1;
for c=y+1:-1:y-1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
c=y-1;
for r=x+1:-1:x-1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
for value=1:3 %search '1'/'2/'3' value pixel arond the minutia point in the window.
for p=x-1:x+1
for q=y-1:y+1
if wind(p,q)==value
xi=p;
yi=q;
end
end
end
cnt=0;
while(xi>x-wl)&&(xi<x+wl)&&(yi>y-wl)&&(yi<y+wl) %mark '1'/'2'/'3' to the adjacent connected pixels
k=0;
for p=xi-1:xi+1
for q=yi-1:yi+1
if (thinned(p,q)==1)&&(wind(p,q)==0)
wind(p,q)=value;
k=k+1;
if k==1
x1=p;
y1=q;
end
if k==2 %more than one connected pixel
cnt=cnt+1;
x2=p;
y2=q;
t1=x1;
x1=x2;
x2=t1;
t2=y1;
y1=y2;
y2=t2;
%___________
xi=x2;
yi=y2;
while(xi>x-wl)&&(xi<x+wl)&&(yi>y-wl)&&(yi<y+wl)
k=0;
flag_see=0;
for p=xi-1:xi+1
for q=yi-1:yi+1
if (thinned(p,q)==1)&&(wind(p,q)==0)
wind(p,q)=value;
flag_see=1;
x3=p;
y3=q;
k=k+1;
end
end
end
if(flag_see==1)
xi=x3;
yi=y3;
else
xi=p;
yi=q;
end
if k==0
break
end
end
% xi=x1;
% yi=y1;
end
end
end
end
xi=x1;
yi=y1;
if k==0
break
end
end
end
% figure,subimage(wind);
%to count 0-1, 0-2, 0-3 transition around the boundary clkwise
T=1;
for v=1:3
T0v=0;
r=x-wl;
for c=y-wl:y+(wl-1)
if (wind(r,c)==0)&&(wind(r,c+1)==v)
T0v=T0v+1;
end
end
c=y+wl;
for r=x-wl:x+(wl-1)
if (wind(r,c)==0)&&(wind(r+1,c)==v)
T0v=T0v+1;
end
end
r=x+wl;
for c=y+wl:-1:y-(wl-1)
if (wind(r,c)==0)&&(wind(r,c-1)==v)
T0v=T0v+1;
end
end
c=y-wl;
for r=x+wl:-1:x-(wl-1)
if (wind(r,c)==0)&&(wind(r-1,c)==v)
T0v=T0v+1;
end
end
T1=T0v==1;
T=T & T1;
end
if T~=1
minutiae_valid(i,:)=[0 0 0 0];
combined(x,y,:)=[255,255,255];
end
end
end
minu_count=1;
minutiae_valid_final(minu_count, :) = [0,0,0,0]; %to make x, y, CN, theta value of minutiae not in border region.
for i=1:totminu_I
CN = minutiae_valid(i,3);
if (CN~=0)
minutiae_valid_final(minu_count, :) = minutiae_valid(i,:);
minu_count = minu_count + 1;
end
end
% totminu_I= minu_count-1;
minutiae_valid = minutiae_valid_final;
% figure, subimage(combined);
At lines 212 and 213 you create nested loops that loop over nine elements of wind looking for elements that match some sort of condition. When you find one, you change the center of your nine element block.
The problem arises when the center of your block is on an edge of wind. When you look over the nine elements centered at an edge you must not access indices in the same way, otherwise you get the error as in above. To illustrate a simple case of this:
a = rand(4,4);
for p=2:4
for q = 1:3
tmp += a(p,q)
end
end
sums the 9 elements of a (a square array) centered at (3,2).
a = rand(4,4);
for p=3:5
for q = 1:3
tmp += a(p,q)
end
end
will generate the same error that you have when it tries to access a(5,1) because this element of the array does not exist. The correct way to fix this depends on what you're trying to do, but that's most definitely beyond the scope of your question, and our ability to figure out.
I hope this helps.
Echoing some of the comments, I'd like to offer you a piece of advice. In the future, you're much more likely to get on taget, quality answers if you can boil your question down to the shortest possible test case. This is also a valuable step in debugging your own code. Figuring out how to construct a test case will often make the answer to your problem apparent. When it's not apparent, Stackoverflow is an excellent resource.