matlab源码网站(求一份基于matlab的TSP问题源代码)

本文目录
- 求一份基于matlab的TSP问题源代码
- 有哪些matlab代码下载网站比较好,推荐一下
- 如何查看matlab函数源代码
- 求matlab中fft函数的源代码
- 求用matlab实现huffman压缩与解压文件源码
- 怎样查看matlab中SVD的源码
- 要怎么看Matlab自带函数的源代码
求一份基于matlab的TSP问题源代码
function =anttsp(city,iter_max,m,Alpha,Beta,Rho,Q)
n=size(city,1);
d=zeros(n,n);
d=squareform(pdist(city));
Eta=1./d;
Tau=ones(n,n);
Tabu=zeros(m,n);
nC=1;
R_best=zeros(iter_max,n);
L_best=inf.*ones(iter_max,1);
while nC《=iter_max
route=;
for i=1:ceil(m/n)
route=;
end
Tabu(:,1)=(route(1,1:m))’;
for j=2:n
for i=1:m
visited=Tabu(i,1:(j-1));
J=zeros(1,(n-j+1));
P=J;
Jc=1;
for k=1:n
if isempty(find(visited==k, 1))
J(Jc)=k;
Jc=Jc+1;
end
end
for k=1:length(J)
P(k)=(Tau(visited(end),J(k))^Alpha)*(Eta(visited(end),J(k))^Beta);
end
P=P/(sum(P));
Pcum=cumsum(P);
Select=find(Pcum》=rand);
if isempty(Select)%是不是一定能保证Select不为空
Tabu(i,j)=round(1+(n-1)*rand);
else
next_visit=J(Select(1));
Tabu(i,j)=next_visit;
end
end
end
if nC》=2
Tabu(1,:)=R_best(nC-1,:);
end
L=zeros(m,1);
for i=1:m
R=Tabu(i,:);
for j=1:(n-1)
L(i)=L(i)+d(R(j),R(j+1));
end
L(i)=L(i)+d(R(1),R(n));
end
L_best(nC)=min(L);
pos=find(L==L_best(nC));
R_best(nC,:)=Tabu(pos(1),:);
nC=nC+1;
Delta_Tau=zeros(n,n);
for i=1:m
for j=1:(n-1)
Delta_Tau(Tabu(i,j),Tabu(i,j+1))=Delta_Tau(Tabu(i,j),Tabu(i,j+1))+Q/L(i);
end
Delta_Tau(Tabu(i,n),Tabu(i,1))=Delta_Tau(Tabu(i,n),Tabu(i,1))+Q/L(i);
end
Tau=(1-Rho).*Tau+Delta_Tau;
Tabu=zeros(m,n);
end
Pos=find(L_best==min(L_best));
Shortest_Route=R_best(Pos(1),:);
Shortest_Length=L_best(Pos(1));
end
有哪些matlab代码下载网站比较好,推荐一下
英文的代码下载请去github;
中文的技术问答请去csdn;
中文的专注matlab代码下载请去downma;
中文其他综合代码下载请去pudn;
以上都可以直接百度搜索到。
如何查看matlab函数源代码
1、在命令窗口中输入:type 函数名(例如type rgb2gray等),就会在命令窗口出现该函数的相关说明及源代码;
2、在命令窗口中输入:open 函数名(例如open rgb2gray等),就会打开该函数的m文件;
3、在命令窗口中输入:edit函数名(例如edit rgb2gray等),同样会打开该函数的m文件;
4、还有一种方法就是故意将原函数的参数类型或者个数写错,就会出现出错提示,点击提示出错处,也可以打开该函数的m文件;
很多大型的函数都能获得源代码,但是MATLAB里面有一些built-in函数是看不到源代码的,sort就是其中之一。这些函数是预编译好的,运行效率非常好,比如像find、min、max等频繁用到的一些函数还有很多矩阵运算函数都是built-in函数。
自带函数,用type+函数名。比如,type dwt2可以显示dwt2函数的代码
求matlab中fft函数的源代码
一般是用:type 函数名,但有些是看不到的比如fft,sin等。
给你个c++实现的fft
#include 《iostream》
#include 《fstream》
#include 《math.h》
using namespace std;
const double PI = 3.14159265358979323846;
int n; // 数据个数 = 2的logn次方
int logn;
/// 复数结构体
struct stCompNum
{
double re;
double im;
};
stCompNum* pData1 = NULL;
stCompNum* pData2 = NULL;
/// Examda提示: 正整数位逆序后输出
int reverseBits(int value, int bitCnt)
{
int i;
int ret = 0;
for(i=0; i《bitCnt; i++)
{
ret |= (value & 0x1) 《《 (bitCnt - 1 - i);
value 》》= 1;
}
return ret;
}
void main()
{
ifstream fin("data.txt");
int i,j,k;
// input logn
fin》》logn;
// calculate n
for(i=0, n=1; i《logn; i++) n *= 2;
// malloc memory space
pData1 = new stCompNum;
pData2 = new stCompNum;
// input raw data
for(i=0; i《n; i++) fin》》pData1.re;
for(i=0; i《n; i++) fin》》pData1.im;
// FFT transform
int cnt = 1;
for(k=0; k《logn; k++)
{
for(j=0; j《cnt; j++)
{
int len = n / cnt;
double c = - 2 * PI / len;
for(i=0; i《len/2; i++)
{
int idx = len * j + i;
pData2.re;
pData2.im;
}
for(i=len/2; i《len; i++)
{
double wcos = cos(c * (i - len/2));
double wsin = sin(c * (i - len/2));
int idx = len * j + i;
stCompNum tmp;
tmp.re = pData1.re;
tmp.im = pData1.im;
pData2.re = tmp.re * wcos - tmp.im * wsin;
pData2.im = tmp.re * wsin + tmp.im * wcos;
}
}
cnt 《《= 1;
stCompNum* pTmp = NULL;
pTmp = pData1;
pData1 = pData2;
pData2 = pTmp;
}
// resort
for(i=0; i《n; i++)
{
int rev = reverseBits(i, logn);
stCompNum tmp;
if(rev 》 i)
{
tmp = pData1;
pData1;
pData1 = tmp;
}
}
// output result data
for(i=0; i《n; i++) cout《《pData1.re《《"\t";
cout《《endl;
for(i=0; i《n; i++) cout《《pData1.im《《"\t";
cout《《endl;
// free memory space
delete pData1;
delete pData2;
fin.close();
system("pause");
}
求用matlab实现huffman压缩与解压文件源码
function =huffman(p)
if (length(find(p《0))~=0)
error(’Not a prob,negative component’);
end
if (abs(sum(p)-1)》10e-10)
error(’Not a prob.vector,component do not add to 1’)
end
n=length(p);
q=p;
m=zeros(n-1,n);
for i=1:n-1
=sort(q);
m(i,:)=;
q=;
end
for i=1:n-1
c(i,:)=blanks(n*n);
end
c(n-1,n)=’0’;
c(n-1,2*n)=’1’;
for i=2:n-1
c(n-i,1:n-1)=c(n-i+1,n*(find(m(n-i+1,:)==1))...
-(n-2):n*(find(m(n-i+1,:)==1)));
c(n-i,n)=’0’;
c(n-i,n+1:2*n-1)=c(n-i,1:n-1);
c(n-i,2*n)=’1’;
for j=1:i-1
c(n-i,(j+1)*n+1:(j+2)*n)=c(n-i+1,...
n*(find(m(n-i+1,:)==j+1)-1)+1:n*find(m(n-i+1,:)==j+1));
end
end
for i=1:n
h(i,1:n)=c(1,n*(find(m(1,:)==i)-1)+1:find(m(1,:)==i)*n);
ll(i)=length(find(abs(h(i,:))~=32));
end
l=sum(p.*ll);
=huffman(p),输入为一维行矩阵p,p为各符号的概率分布,概率和为1,各元素值为
正,输出H矩阵为对应每个符号概率的码字,L为输出码字的平均码长。Huffman .m运用典
型的IF和FOR控制流循环语句,该程序包括两个IF 控制流和5个FOR 循环结构。
第一个IF 语句判断输入P矩阵各元素是否全为大于零的有效概率值;第二个IF 语句判断
输入矩阵的概率和是否为合理值1。
N取输入行向量P的长度,即需要编码元素个数。
M为N-1行、N列矩阵,用来记录每行最小两概率叠加后概率排列次序。
第一个FOR 循环确定概率大小值的排列,得到M矩阵。
第二个FOR循环生成一个N-1行、N2(N×N)列矩阵C,每行可看作N个段,每段长为N,记
录一个码字(每个码字的长度不会超过N)。
给C矩阵的N-1行的第一个段赋值0,第二个段赋值1,这两个码字对应编码中最后相加为一
的两个概率。
第三个循环是本程序的主要部分,循环N-2次,决定矩阵C从倒数第二行开始到第一行的每
段的码字值。每一行值都从下一行值得到,找到在下一行码字中相加本行最小两个概率得
到的概率的对应码字,本行两个最小概率对应码字分别为此码字最后加“0”,加“1”。
嵌套的第四个FOR循环找到其余的本行在下一行对应的码字,该码字保持不变。循环结束
后,C矩阵第一行的N段对应输入N个概率所对应符号的码字。该码字按码字长短排列。
第五个FOR循环根据M矩阵第一行记录的概率排序位置分配给每个概率对应符号的码字。
FOR EXAMPLE:
P=;
N=4;
M矩阵:
m =
N矩阵:
n =[1110 1110 110 0;
10 11 0 ;
0 1 ]
注:huffman.m from 《《contemporary communication systems using MATLAB》》
written by John G.Proakis ,Masoud Salehi
怎样查看matlab中SVD的源码
自带的有加密看不了,看看mathworks公司网站上会员分享的:
==========================================================
function = svdsim(a,tol)
%SVDSIM simple SVD program
%
% A simple program that demonstrates how to use the
% QR decomposition to perform the SVD of a matrix.
% A may be rectangular and complex.
%
% usage: = SVDSIM(A)
% or S = SVDSIM(A)
%
% with A = U*S*V’ , S》=0 , U’*U = Iu , and V’*V = Iv
%
% The idea is to use the QR decomposition on A to gradually "pull" U out from
% the left and then use QR on A transposed to "pull" V out from the right.
% This process makes A lower triangular and then upper triangular alternately.
% Eventually, A becomes both upper and lower triangular at the same time,
% (i.e. Diagonal) with the singular values on the diagonal.
%
% Matlab’s own SVD routine should always be the first choice to use,
% but this routine provides a simple "algorithmic alternative"
% depending on the users’ needs.
%
%see also: SVD, EIG, QR, BIDIAG, HESS
%
% Paul Godfrey
% October 23, 2006
if ~exist(’tol’,’var’)
tol=eps*1024;
end
%reserve space in advance
sizea=size(a);
loopmax=100*max(sizea);
loopcount=0;
% or use Bidiag(A) to initialize U, S, and V
u=eye(sizea(1));
s=a’;
v=eye(sizea(2));
Err=realmax;
while Err》tol & loopcount《loopmax ;
% log10(); pause
=qr(s’); u=u*q;
=qr(s’); v=v*q;
% exit when we get "close"
e=triu(s,1);
E=norm(e(:));
F=norm(diag(s));
if F==0, F=1;end
Err=E/F;
loopcount=loopcount+1;
end
%
%fix the signs in S
ss=diag(s);
s=zeros(sizea);
for n=1:length(ss)
ssn=ss(n);
s(n,n)=abs(ssn);
if ssn《0
u(:,n)=-u(:,n);
end
end
if nargout《=1
u=diag(s);
end
return
要怎么看Matlab自带函数的源代码
MATALB属于半开源软件,其中很多函数可以通过“open/edit/type+filename”命令进行和查看源代码。但是有一些函数仅仅可以找到它的帮助文档,却无法看到具体的源代码,比如min,fft,sum等函数,因为这些函数属于MATLAB的built-in function(内置函数),即MATLAB的built-in
function的代码是不公开的,有人说这些函数的算法是最优化的,保证较低的时间复杂度提高效率,所以,我感觉写一个算法优先考虑调用MATLAB自带函数,自带函数解决不了的情况下,再自己去写,毕竟自己写的代码的效率无法达到最优。

更多文章:
matlab源码网站(求一份基于matlab的TSP问题源代码)
2026年10月19日 21:00
page对象的常用属性(getPageSize()中对象的属性是怎么回事)
2026年10月19日 13:40
plc中xor是什么指令(假设BL寄存器中的低4位不变,高4位取反,使用什么指令)
2026年10月19日 12:20
schema翻译(数据库中的”SCHEMA”是什么,怎么翻译)
2026年10月19日 11:10
高级语言程序设计是c语言吗(高级语言程序设计C与C++有什么区别吗,是不是难度等级不一样啊)
2026年10月19日 10:40
10进制转换成2进制在线(十进制数怎么换算成二进制数、在线等)
2026年10月19日 08:50




