A. 跪求快速中值濾波演算法matlab源代碼
你注意了,imread(路徑,'name.jpg'),我是以我電腦的圖片給你做的,你運行時候,MATLAB路徑要改到你需要處理圖片的路徑。
代碼如下:
I=imread('11.jpg');%讀取圖像,
subplot(2,2,1),imshow(I);title('原圖');%顯示原圖像
J=rgb2gray(I);%把彩色圖像轉化為灰度圖像
subplot(2,2,2),imshow(J);title('灰度圖');%顯示灰度圖像
J=imnoise(J,'salt&pepper',0.005);%加上椒鹽雜訊
subplot(2,2,3),imshow(J);title('椒鹽雜訊圖');%顯示加上椒鹽的圖像
H=medfilt2(J);%中值濾波
subplot(2,2,4),imshow(H);title('處理後圖');%顯示中值濾波後的圖像
中值濾波器適合於椒鹽濾波,均值濾波器適合於高斯雜訊
希望能幫到你!
B. 求MATLAB代碼
MATLAB實用源代碼
1圖像的讀取及旋轉
A=imread('');%讀取圖像
subplot(2,2,1),imshow(A),title('原始圖像');%輸出圖像
I=rgb2gray(A);
subplot(2,2,2),imshow(A),title('灰度圖像');
subplot(2,2,3),imhist(I),title('灰度圖像直方圖');%輸出原圖直方圖
theta = 30;J = imrotate(I,theta);% Try varying the angle, theta.
subplot(2,2,4), imshow(J),title(『旋轉圖像』)
2邊緣檢測
I=imread('C:\Users\HP\Desktop\平時總結\路飛.jpg');
subplot(2,2,1),imshow(I),title('原始圖像');
I1=edge(I,'sobel');
subplot(2,2,2),imshow(I1),title('sobel邊緣檢測');
I2=edge(I,'prewitt');
subplot(2,2,3),imshow(I2),title('prewitt邊緣檢測');
I3=edge(I,'log');
subplot(2,2,4),imshow(I3),title('log邊緣檢測');
3圖像反轉
MATLAB 程序實現如下:
I=imread('xian.bmp');
J=double(I);
J=-J+(256-1);%圖像反轉線性變換
H=uint8(J);
subplot(1,2,1),imshow(I);
subplot(1,2,2),imshow(H);
4.灰度線性變換
MATLAB 程序實現如下:
I=imread('xian.bmp');
subplot(2,2,1),imshow(I);
title('原始圖像');
axis([50,250,50,200]);
axis on;%顯示坐標系
I1=rgb2gray(I);
subplot(2,2,2),imshow(I1);
title('灰度圖像');
axis([50,250,50,200]);
axis on; %顯示坐標系
J=imadjust(I1,[0.1 0.5],[]); %局部拉伸,把[0.1 0.5]內的灰度拉伸為[0 1]
subplot(2,2,3),imshow(J);
title('線性變換圖像[0.1 0.5]');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
K=imadjust(I1,[0.3 0.7],[]); %局部拉伸,把[0.3 0.7]內的灰度拉伸為[0 1]
subplot(2,2,4),imshow(K);
title('線性變換圖像[0.3 0.7]');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
5.非線性變換
MATLAB 程序實現如下:
I=imread('xian.bmp');
I1=rgb2gray(I);
subplot(1,2,1),imshow(I1);
title(' 灰度圖像');
axis([50,250,50,200]);
grid on;%顯示網格線
axis on;%顯示坐標系
J=double(I1);
J=40*(log(J+1));
H=uint8(J);
subplot(1,2,2),imshow(H);
title(' 對數變換圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
4.直方圖均衡化
MATLAB 程序實現如下:
I=imread('xian.bmp');
I=rgb2gray(I);
figure;
subplot(2,2,1);
imshow(I);
subplot(2,2,2);
imhist(I);
I1=histeq(I);
figure;
subplot(2,2,1);
imshow(I1);
subplot(2,2,2);
imhist(I1);
5. 線性平滑濾波器
用MATLAB實現領域平均法抑制雜訊程序:
I=imread('xian.bmp');
subplot(231)
imshow(I)
title('原始圖像')
I=rgb2gray(I);
I1=imnoise(I,'salt & pepper',0.02);
subplot(232)
imshow(I1)
title(' 添加椒鹽雜訊的圖像')
k1=filter2(fspecial('average',3),I1)/255; %進行3*3模板平滑濾波
k2=filter2(fspecial('average',5),I1)/255; %進行5*5模板平滑濾波k3=filter2(fspecial('average',7),I1)/255; %進行7*7模板平滑濾波
k4=filter2(fspecial('average',9),I1)/255; %進行9*9模板平滑濾波
subplot(233),imshow(k1);title('3*3 模板平滑濾波');
subplot(234),imshow(k2);title('5*5 模板平滑濾波');
subplot(235),imshow(k3);title('7*7 模板平滑濾波');
subplot(236),imshow(k4);title('9*9 模板平滑濾波');
6.中值濾波器
用MATLAB實現中值濾波程序如下:
I=imread('xian.bmp');
I=rgb2gray(I);
J=imnoise(I,'salt&pepper',0.02);
subplot(231),imshow(I);title('原圖像');
subplot(232),imshow(J);title('添加椒鹽雜訊圖像');
k1=medfilt2(J); %進行3*3模板中值濾波
k2=medfilt2(J,[5,5]); %進行5*5模板中值濾波
k3=medfilt2(J,[7,7]); %進行7*7模板中值濾波
k4=medfilt2(J,[9,9]); %進行9*9模板中值濾波
subplot(233),imshow(k1);title('3*3模板中值濾波');
subplot(234),imshow(k2);title('5*5模板中值濾波 ');
subplot(235),imshow(k3);title('7*7模板中值濾波');
subplot(236),imshow(k4);title('9*9 模板中值濾波');
7.用Sobel運算元和拉普拉斯對圖像銳化:
I=imread('xian.bmp');
subplot(2,2,1),imshow(I);
title('原始圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on;%顯示坐標系
I1=im2bw(I);
subplot(2,2,2),imshow(I1);
title('二值圖像');
axis([50,250,50,200]);
grid on;%顯示網格線
axis on;%顯示坐標系
H=fspecial('sobel');%選擇sobel運算元
J=filter2(H,I1); %卷積運算
subplot(2,2,3),imshow(J);
title('sobel運算元銳化圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on;%顯示坐標系
h=[0 1 0,1 -4 1,0 1 0]; %拉普拉斯運算元
J1=conv2(I1,h,'same');%卷積運算
subplot(2,2,4),imshow(J1);
title('拉普拉斯運算元銳化圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
8.梯度運算元檢測邊緣
用 MATLAB實現如下:
I=imread('xian.bmp');
subplot(2,3,1);
imshow(I);
title('原始圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
I1=im2bw(I);
subplot(2,3,2);
imshow(I1);
title('二值圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
I2=edge(I1,'roberts');
figure;
subplot(2,3,3);
imshow(I2);
title('roberts運算元分割結果');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
I3=edge(I1,'sobel');
subplot(2,3,4);
imshow(I3);
title('sobel運算元分割結果');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
I4=edge(I1,'Prewitt');
subplot(2,3,5);
imshow(I4);
title('Prewitt運算元分割結果 ');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
9.LOG運算元檢測邊緣
用 MATLAB程序實現如下:
I=imread('xian.bmp');
subplot(2,2,1);
imshow(I);
title('原始圖像');
I1=rgb2gray(I);
subplot(2,2,2);
imshow(I1);
title('灰度圖像');
I2=edge(I1,'log');
subplot(2,2,3);
imshow(I2);
title('log運算元分割結果');
10.Canny運算元檢測邊 緣
用MATLAB程序實現如下:
I=imread('xian.bmp');
subplot(2,2,1);
imshow(I);
title('原始圖像')
I1=rgb2gray(I);
subplot(2,2,2);
imshow(I1);
title('灰度圖像');
I2=edge(I1,'canny');
subplot(2,2,3);
imshow(I2);
title('canny運算元分割結果');
11.邊界跟蹤 (bwtraceboundary函數)
clc
clear all
I=imread('xian.bmp');
figure
imshow(I);
title('原始圖像');
I1=rgb2gray(I); %將彩色圖像轉化灰度圖像
threshold=graythresh(I1); %計算將灰度圖像轉化為二值圖像所需的門限
BW=im2bw(I1, threshold); %將灰度圖像轉化為二值圖像
figure
imshow(BW);
title('二值圖像');
dim=size(BW);
col=round(dim(2)/2)-90; %計算起始點列坐標
row=find(BW(:,col),1); %計算起始點行坐標
connectivity=8;
num_points=180;
contour=bwtraceboundary(BW,[row,col],'N',connectivity,num_points);
%提取邊界
figure
imshow(I1);
hold on;
plot(contour(:,2),contour(:,1), 'g','LineWidth' ,2);
title('邊界跟蹤圖像');
12.Hough變換
I= imread('xian.bmp');
rotI=rgb2gray(I);
subplot(2,2,1);
imshow(rotI);
title('灰度圖像');
axis([50,250,50,200]);
grid on;
axis on;
BW=edge(rotI,'prewitt');
subplot(2,2,2);
imshow(BW);
title('prewitt運算元邊緣檢測 後圖像');
axis([50,250,50,200]);
grid on;
axis on;
[H,T,R]=hough(BW);
subplot(2,2,3);
imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
title('霍夫變換圖');
xlabel('\theta'),ylabel('\rho');
axis on , axis normal, hold on;
P=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x=T(P(:,2));y=R(P(:,1));
plot(x,y,'s','color','white');
lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
subplot(2,2,4);,imshow(rotI);
title('霍夫變換圖像檢測');
axis([50,250,50,200]);
grid on;
axis on;
hold on;
max_len=0;
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
len=norm(lines(k).point1-lines(k).point2);
if(len>max_len)
max_len=len;
xy_long=xy;
end
end
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
13.直方圖閾值法
用 MATLAB實現直方圖閾值法:
I=imread('xian.bmp');
I1=rgb2gray(I);
figure;
subplot(2,2,1);
imshow(I1);
title(' 灰度圖像')
axis([50,250,50,200]);
grid on;%顯示網格線
axis on; %顯示坐標系
[m,n]=size(I1);%測量圖像尺寸參數
GP=zeros(1,256); %預創建存放灰度出現概率的向量
for k=0:255
GP(k+1)=length(find(I1==k))/(m*n);%計算每級灰度出現的概率,將其存入GP中相應位置
end
subplot(2,2,2),bar(0:255,GP,'g')%繪制直方圖
title('灰度直方圖')
xlabel('灰度值')
ylabel(' 出現概率')
I2=im2bw(I,150/255);
subplot(2,2,3),imshow(I2);
title('閾值150的分割圖像')
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
I3=im2bw(I,200/255); %
subplot(2,2,4),imshow(I3);
title('閾值200的分割圖像')
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
14. 自動閾值法:Otsu法
用MATLAB實現Otsu演算法:
clc
clear all
I=imread('xian.bmp');
subplot(1,2,1),imshow(I);
title('原始圖像')
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
level=graythresh(I); %確定灰度閾值
BW=im2bw(I,level);
subplot(1,2,2),imshow(BW);
title('Otsu 法閾值分割圖像')
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
15.膨脹操作
I=imread('xian.bmp'); %載入圖像
I1=rgb2gray(I);
subplot(1,2,1);
imshow(I1);
title('灰度圖像')
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
se=strel('disk',1); %生成圓形結構元素
I2=imdilate(I1,se); %用生成的結構元素對圖像進行膨脹
subplot(1,2,2);
imshow(I2);
title(' 膨脹後圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
16.腐蝕操作
MATLAB 實現腐蝕操作
I=imread('xian.bmp'); %載入圖像
I1=rgb2gray(I);
subplot(1,2,1);
imshow(I1);
title('灰度圖像')
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
se=strel('disk',1); %生成圓形結構元素
I2=imerode(I1,se); %用生成的結構元素對圖像進行腐蝕
subplot(1,2,2);
imshow(I2);
title('腐蝕後圖像');
axis([50,250,50,200]);
grid on; %顯示網格線
axis on; %顯示坐標系
17.開啟和閉合操作
用 MATLAB實現開啟和閉合操作
I=imread('xian.bmp'); %載入圖像
subplot(2,2,1),imshow(I);
title('原始圖像');
axis([50,250,50,200]);
axis on; %顯示坐標系
I1=rgb2gray(I);
subplot(2,2,2),imshow(I1);
title('灰度圖像');
axis([50,250,50,200]);
axis on; %顯示坐標系
se=strel('disk',1); %採用半徑為1的圓作為結構元素
I2=imopen(I1,se); %開啟操作
I3=imclose(I1,se); %閉合操作
subplot(2,2,3),imshow(I2);
title('開啟運算後圖像');
axis([50,250,50,200]);
axis on; %顯示坐標系
subplot(2,2,4),imshow(I3);
title('閉合運算後圖像');
axis([50,250,50,200]);
axis on; %顯示坐標系
18.開啟和閉合組合操作
I=imread('xian.bmp');%載入圖像
subplot(3,2,1),imshow(I);
title('原始圖像');
axis([50,250,50,200]);
axis on;%顯示坐標系
I1=rgb2gray(I);
subplot(3,2,2),imshow(I1);
title('灰度圖像');
axis([50,250,50,200]);
axis on;%顯示坐標系
se=strel('disk',1);
I2=imopen(I1,se);%開啟操作
I3=imclose(I1,se);%閉合操作
subplot(3,2,3),imshow(I2);
title('開啟運算後圖像');
axis([50,250,50,200]);
axis on;%顯示坐標系
subplot(3,2,4),imshow(I3);
title('閉合運算後圖像');
axis([50,250,50,200]);
axis on;%顯示坐標系
se=strel('disk',1);
I4=imopen(I1,se);
I5=imclose(I4,se);
subplot(3,2,5),imshow(I5);%開—閉運算圖像
title('開—閉運算圖像');
axis([50,250,50,200]);
axis on;%顯示坐標系
I6=imclose(I1,se);
I7=imopen(I6,se);
subplot(3,2,6),imshow(I7);%閉—開運算圖像
title('閉—開運算圖像');
axis([50,250,50,200]);
axis on;%顯示坐標系
19.形態學邊界提取
利用 MATLAB實現如下:
I=imread('xian.bmp');%載入圖像
subplot(1,3,1),imshow(I);
title('原始圖像');
axis([50,250,50,200]);
grid on;%顯示網格線
axis on;%顯示坐標系
I1=im2bw(I);
subplot(1,3,2),imshow(I1);
title('二值化圖像');
axis([50,250,50,200]);
grid on;%顯示網格線
axis on;%顯示坐標系
I2=bwperim(I1); %獲取區域的周長
subplot(1,3,3),imshow(I2);
title('邊界周長的二值圖像');
axis([50,250,50,200]);
grid on;
axis on;
20.形態學骨架提取
利用MATLAB實現如下:
I=imread('xian.bmp');
subplot(2,2,1),imshow(I);
title('原始圖像');
axis([50,250,50,200]);
axis on;
I1=im2bw(I);
subplot(2,2,2),imshow(I1);
title('二值圖像');
axis([50,250,50,200]);
axis on;
I2=bwmorph(I1,'skel',1);
subplot(2,2,3),imshow(I2);
title('1次骨架提取');
axis([50,250,50,200]);
axis on;
I3=bwmorph(I1,'skel',2);
subplot(2,2,4),imshow(I3);
title('2次骨架提取');
axis([50,250,50,200]);
axis on;
21.直接提取四個頂點坐標
I = imread('xian.bmp');
I = I(:,:,1);
BW=im2bw(I);
figure
imshow(~BW)
[x,y]=getpts
平滑濾波
h=fspecial('average',9);
I_gray=imfilter(I_gray,h,'replicate');%平滑濾波
C. matlab源代碼
hrollfcoef這個函數不是matlab自帶的
function [xh] = hrollfcoef(irfn,ipoint,sr,alfs,ncc)
%****************** variables *************************
% irfn : Number of symbols to use filtering
% ipoint : Number of samples in one symbol
% sr : symbol rate
% alfs : rolloff coeficiense
% ncc : 1 -- transmitting filter 0 -- receiving filter
% *****************************************************
xi=zeros(1,irfn*ipoint+1);
xq=zeros(1,irfn*ipoint+1);
point = ipoint;
tr = sr ;
tstp = 1.0 ./ tr ./ ipoint;
n = ipoint .* irfn;
mid = ( n ./ 2 ) + 1;
sub1 = 4.0 .* alfs .* tr; % 4*alpha*R_s
for i = 1 : n
icon = i - mid;
ym = icon;
if icon == 0.0
xt = (1.0-alfs+4.0.*alfs./pi).* tr; % h(0)
else
sub2 =16.0.*alfs.*alfs.*ym.*ym./ipoint./ipoint;
if sub2 ~= 1.0
x1=sin(pi*(1.0-alfs)/ipoint*ym)./pi./(1.0-sub2)./ym./tstp;
x2=cos(pi*(1.0+alfs)/ipoint*ym)./pi.*sub1./(1.0-sub2);
xt = x1 + x2; % h(t) plot((1:length(xh)),xh)
else % (4alphaRst)^2 = 1plot((1:length(xh)),xh)
xt = alfs.*tr.*((1.0-2.0/pi).*cos(pi/4.0/alfs)+(1.0+2.0./pi).*sin(pi/4.0/alfs))./sqrt(2.0);
end % if sub2 ~= 1.0
end % if icon == 0.0
if ncc == 0 % in the case of receiver
xh( i ) = xt ./ ipoint ./ tr; % normalization
elseif ncc == 1 % in the case of transmitter
xh( i ) = xt ./ tr; % normalization
else
error('ncc error');
end % if ncc == 0
end % for i = 1 : n
%******************** end of file ***************************
網上找的,你看看能不能拼到你那個程序里去
D. matlab編程問題:ARMA和MUSIC
主要問題包括:
1、變數x未定義。
2、函數MUSIC裡面:
S=[S(257:512)S(1:256)];
應為
S=[S(257:512)S(1:256)];
另外,clearR未定義,不知道干什麼用的,可以直接刪掉。
3、函數ARMA裡面,調用的Burg未定義。
E. 用Matlab實現apriori演算法關聯規則的挖掘程序,完整有詳細註解
下面這段是apriori演算法中由2頻繁項集找k頻繁項集的程序,程序中有兩個問題:
1、似乎while循環的K永遠都是固定的,也就是都是頻繁2項集的個數。得到頻繁3項集後K的個數不是要變嗎?如何體現呢?
2、程序中有兩個for的大循環,但是發現結果是只要找到一個頻繁3項集第二個for循環就會結束,但是其實還應該有其它的頻繁3項集。for循環不是應該無條件執行到參數k結束嗎?當時k值是15,可是程序結束的時候i=2,j=3,然後j就不執行4以及一直到k的部分了。是什麼原因呢?麻煩高手指點一下。急啊……
while( k>0)
le=length(candidate{1});
num=2;
nl=0;
for i=1:k-1
for j=i+1:k
x1=candidate{i}; %candidate初始值為頻繁2項集,這個表示頻繁項集的第i項
x2=candidate{j};
c = intersect(x1, x2);
M=0;
r=1;
nn=0;
l1=0;
if (length(c)==le-1) & (sum(c==x1(1:le-1))==le-1)
houxuan=union(x1(1:le),x2(le));
%樹剪枝,若一個候選項的某個K-1項子集為非頻繁,則剪枝掉
sub_set=subset(houxuan);
%生成該候選項的所有K-1項子集
NN=length(sub_set);
%判斷這些K-1項自己是否都為頻繁的
while(r & M<NN)
M=M+1;
r=in(sub_set{M},candidate);
end
if M==NN
nl=nl+1;
%候選k項集
cand{nl}=houxuan;
%記錄每個候選k項集出現的次數
le=length(cand{1});
for i=1:m
s=cand{nl};
x=X(i,:);
if sum(x(s))==le
nn=nn+1;
end
end
end
end
%從候選集中找頻繁項集
if nn>=th
ll=ll+1;
candmid{nl}=cand{nl};
pfxj(nl).element=cand{nl};
pfxj(nl).time=nn;
disp('得到的頻繁項集為:')
result=(candmid{nl});
disp(result);
end
end
end
end
F. 誰能給我舉一個模擬退火演算法MATLAB源代碼的簡單例子
clear
clc
a = 0.95
k = [5;10;13;4;3;11;13;10;8;16;7;4];
k = -k; % 模擬退火演算法是求解最小值,故取負數
d = [2;5;18;3;2;5;10;4;11;7;14;6];
restriction = 46;
num = 12;
sol_new = ones(1,num); % 生成初始解
E_current = inf;E_best = inf;
% E_current是當前解對應的目標函數值(即背包中物品總價值);
% E_new是新解的目標函數值;
% E_best是最優解的
sol_current = sol_new; sol_best = sol_new;
t0=97; tf=3; t=t0;
p=1;
while t>=tf
for r=1:100
%產生隨機擾動
tmp=ceil(rand.*num);
sol_new(1,tmp)=~sol_new(1,tmp);
%檢查是否滿足約束
while 1
q=(sol_new*d <= restriction);
if ~q
p=~p; %實現交錯著逆轉頭尾的第一個1
tmp=find(sol_new==1);
if p
sol_new(1,tmp)=0;
else
sol_new(1,tmp(end))=0;
end
else
break
end
end
% 計算背包中的物品價值
E_new=sol_new*k;
if E_new<E_current
E_current=E_new;
sol_current=sol_new;
if E_new<E_best
% 把冷卻過程中最好的解保存下來
E_best=E_new;
sol_best=sol_new;
end
else
if rand<exp(-(E_new-E_current)./t)
E_current=E_new;
sol_current=sol_new;
else
sol_new=sol_current;
end
end
end
t=t.*a;
end
disp('最優解為:')
sol_best
disp('物品總價值等於:')
val=-E_best;
disp(val)
disp('背包中物品重量是:')
disp(sol_best * d)
G. 求利用matlab求從A到O的最短路徑的程序代碼~~~
functionR=main_Dj()
clc;clear
G=[125;141;231;246;255.8;265.7;275.6;372;3111.5;3124;...
450.5;483;561;593;670.6;6102.5;7112.7;891;8126;...
9101.5;9125;10110.5;10124;11123];
opt=0;
route=sroute(G,opt);
R=[];
r=route(3,end);
R=[r,R];
whiler~=1
r=route(3,r);
R=[r,R];
end
R=char(R+64);
R=[R,'O'];
end
functionroute=sroute(G,opt)
%求圖的最短路的Dijkstra演算法,規定1是起點
%G是給定圖的鄰接矩陣或弧表矩陣,程序能夠自動識別
%當opt=0(或預設)時求無向圖的最短路,opt=1時求有向圖的最短路
%d——標記最短距離
%route是一個矩陣,第一行標記頂點,第2行標記1到該點的最短距離,
%第3行標記最短路上該點的先驅頂點
if(nargin==1)opt=0;end
while1%此循環自動識別或由弧表矩陣生成鄰接矩陣
ifG(1,1)==0
A=G;
n=size(A,1);
M=sum(sum(A));break
else
e=G;
n=max([e(:,1);e(:,2)]);%頂點數
m=size(e,1);%邊數
M=sum(e(:,3));%代表無窮大
A=M*ones(n,n);
fork=1:m
A(e(k,1),e(k,2))=e(k,3);
ifopt==0
A(e(k,2),e(k,1))=e(k,3);%形成無向圖的鄰接矩陣
end
end
A=A-M*eye(n);%形成圖的鄰接矩陣
end
break
end
pb(1:length(A))=0;pb(1)=1;%永久標號點記為1
index1=1;%依次記錄永久標號頂點
index2=ones(1,length(A));%標記最短路上各點的先驅頂點
d(1:length(A))=M;d(1)=0;%標記距離
temp=1;%標記最近一個永久標號點
whilesum(pb)<length(A)
tb=find(pb==0);%找出臨時標號點
d(tb)=min(d(tb),d(temp)+A(temp,tb));%更新距離
tmpb=find(d(tb)==min(d(tb)));%確定新最小距離點
temp=tb(tmpb(1));%其中之一記為新永久標號點
pb(temp)=1;%增加新永久標號點
index1=[index1,temp];%記錄新永久標號點
index=index1(find(d(index1)==d(temp)-A(index1,temp)'));%確定前驅頂點
iflength(index)>=2%前驅頂點多於1個時取第一個
index=index(1);
end
index2(temp)=index;%記錄前驅頂點
end
route=[1:n;d;index2];
end
運行結果
R=
ADEFJKO
代碼自己看,不解釋,也別叫我解釋了,很麻煩的。