发布时间:2023-05-25 15:00
b = a.'
b = a(:)
b = a(:).'
clear classes
[~, idx] = max(a)
[tmp, idx] = max(a); clear tmp
[idx, idx] = max(a)
b = squeeze(a)
tic; some_code_to_run(); toc
bench
diary on
% Lots of my Matlab commands here.
diary off
b = fliplr(a) % For row-vector a.
b = flipud(a) % For column-vector a.
b = wrev(a) % For any vector a.
b = a(end:-1:1); % This is the implementation of function wrev.
a(isnan(a)) = []
function r = fmat(x)
r = x.^2 + 1./x;
end
A = [1 2 3 4];
fmat(A)
function r = fmat2(x)
if x > 0
r = x.^2;
else
r = 1./x;
end
fmat2(A) % 会出错
% 法1
for i = 1:length(A)
r(i) = fmat2(A(i));
end
% 法2 用arrayfun
r = arrayfun(@fmat2, A)
% 法3 用逻辑矩阵
r = zeros(1,length(A));
l1 = (A>0);
l2 = ~l1;
r(l1) = A(l1).^2;
r(l2) = 1./A(l2);
str2func('@(x,y)sin(x*y)') % str -> @func 返回一个函数句柄
syms x y
fs(x,y) = x^2+sin(x*y);
fh=matlabFunction(fs); % symfun -> @func 返回一个函数句柄 !!!强烈推荐
fh = @(x)x.^2+sin(x);
fun2str(fh) % @func ->str 函数句柄变为字符
[x, y] = cylinder(linspace(0,10,100),200) % linspace(0,10,100)指在半径[0, 10]上划分为100份,参数200指的是在圆周方向上 200等分
z = sin(x) + cos(y);
mesh(x, y, z);
function r = fmat2(x)
if x > 0
r = x.^2;
else
r = 1./x;
end
fmat2(A) % 会出错
fmat3 = @(x)x.^2.*(x>0) + 1./x.*(x<=0);
fmat3(A)
syms x;
text(.5,.5,['$',latex(x^(2*x^x+x/3)),'$'],'interpreter','latex','HorizontalAlignment','center','fontsize',18)
sp=actxserver('SAPI.SpVoice');
sp.Speak('hello')
function x = calcuProjOfPQ(CPs,VSPs)
x = zeros(size(CPs,1),size(VSPs,1),3);
for k = 1 : size(CPs,1)
for h = 1 : size(VSPs,1)
for i = 1 : 3
x(k,h,i) = CPs(k, i+1) - VSPs(h, i+1);
end
end
end
end
function x = revised_calcuProjOfPQ(CPs,VSPs)
x = bsxfun(@minus, reshape(CPs(:, 2:end), [], 1, 3), reshape(VSPs(:, 2:end), 1, [], 3));
end
function reTheta = calcuTheta(X,R,Nor)
[m,n,~] = size(X);
reTheta = zeros(m,n);
for i = 1 : m
for j = 1 : n
reTheta(i,j) = X(i,j,1)/R(i,j) * Nor(i,1) + ...
X(i,j,2)/R(i,j) * Nor(i,2) + ...
X(i,j,3)/R(i,j) * Nor(i,3);
end
end
end
function reTheta = revised_calcuTheta(X,R,Nor)
reTheta = sum(bsxfun(@times, bsxfun(@rdivide, X, R), reshape(Nor, [], 1, 3)), 3);
end
function [Uij,Tij] = calcuUijTij(shearM, poisson, R, X, Nor, cosTheta)
Uij = zeros(size(X,1)*3,size(X,2)*3);
Tij = zeros(size(X,1)*3,size(X,2)*3);
elementUij = zeros(3,3);
elementTij = zeros(3,3);
% ganerate Uij and Tij
for a = 1 : size(X,1)
for b = 1 : size(X,2)
for i = 1 : size(X,3)
for j = 1 : size(X,3)
if i == j
detaij = 1;
else
detaij = 0;
end
elementUij(i,j) = 1 / (16*pi*shearM*(1-poisson) * R(a,b)) * ...
((3-4*poisson) * detaij +X(a,b,i)*X(a,b,j)/(R(a,b))^2);
elementTij(i,j) = -1/(8*pi*(1-poisson)*R(a,b)^2) * (((1-2 * ...
poisson) * detaij + 3*X(a,b,i) * X(a,b,j)/R(a,b)^2) * ...
cosTheta(a,b) - (1-2*poisson) * (( X(a,b,i) * Nor(a,j)) ...
/ R(a,b)- X(a,b,j) * Nor(a,i) / R(a,b)));
end
end
Uij(a*3-2:a*3,b*3-2:b*3) = elementUij;%Uij(a*3-2:a*3,b*3-2:b*3) +
Tij(a*3-2:a*3,b*3-2:b*3) = elementTij;%Tij(a*3-2:a*3,b*3-2:b*3) +
end
end
end
function [Uij, Tij] = revised_calcuUijTij(shearM, poisson, R, X, Nor, cosTheta)
[ir, jr] = size(R); [ix, jx, kx] = size(X);
R = reshape(R, 1,1,ir,jr); cosTheta = reshape(cosTheta, 1,1,ir,jr);
X1 = reshape(permute(X, [3,1,2]), [],1,ix,jx); X2 = reshape(permute(X, [3,1,2]), 1,[],ix,jx);
tmp = bsxfun(@rdivide, bsxfun(@times, X1, X2), R.^2);
Uij = reshape(permute(reshape(permute(bsxfun(@rdivide, bsxfun(@plus, tmp, (3-4*poisson)*eye(kx)), R) / (16*pi*shearM*(1-poisson)), [1,2,4,3]), 3,[],ix), [2,1,3]), [],3*ix).';
Tij = bsxfun(@times, 3*tmp, cosTheta) - (1-2*poisson) * bsxfun(@rdivide, (bsxfun(@times, X1, reshape(Nor.', 1,3,[])) - bsxfun(@times, X2, reshape(Nor.', 3,1,[]))), R);
Tij = reshape(permute(reshape(permute(bsxfun(@rdivide, bsxfun(@plus, Tij, bsxfun(@times, (1-2*poisson)*eye(kx), cosTheta)), R.^2) / (8*pi*(poisson-1)), [1,2,4,3]), 3,[],ix), [2,1,3]), [],3*ix).';
end
function [ul, pl] = calcuUlPl(~) %alpha, Uij, Tij
global m n Uij Tij alpha
disp = zeros(size(Uij,1),1);
fors = zeros(size(Uij,1),1);
ulData1 = 0;
ulData2 = 0;
ulData3 = 0;
plData1 = 0;
plData2 = 0;
plData3 = 0;
for j = 1 : m
for i = 1 : n
ulData1 = ulData1 + alpha(i,:) * Uij(j*3-2:j*3,i*3-2);
ulData2 = ulData2 + alpha(i,:) * Uij(j*3-2:j*3,i*3-1);
ulData3 = ulData3 + alpha(i,:) * Uij(j*3-2:j*3,i*3-0);
% =========================================================================
plData1 = plData1 + alpha(i,:) * Tij(j*3-2:j*3,i*3-2);
plData2 = plData2 + alpha(i,:) * Tij(j*3-2:j*3,i*3-1);
plData3 = plData3 + alpha(i,:) * Tij(j*3-2:j*3,i*3-0);
end
disp(j*3-2:j*3,1) =[ulData1;
ulData2;
ulData3];
% =========================================================================
fors(j*3-2:j*3,1) =[plData1;
plData2;
plData3];
%% ==================================================================
% Forget to zero the accumulation variable, I spent nearly half a month of
% time to debug the code
ulData1 = 0;
ulData2 = 0;
ulData3 = 0;
plData1 = 0;
plData2 = 0;
plData3 = 0;
%% ==================================================================
end
sum(disp)
sum(fors)
% reshape the ul and pl to 2 mx3 martices respectively
ul = zeros(m,3);pl = zeros(m,3);
ul = reshape(disp,[3,numel(disp)/3])';
pl = reshape(fors,[3,numel(fors)/3])';
end
function [ul, pl] = revised_calcuUlPl(Uij,Tij,alpha)
n = size(Uij, 2);
alpha = reshape(alpha.',3,1,[]);
ul = reshape(sum(sum(bsxfun(@times, alpha, reshape(permute(reshape(Uij.', n,3,[]), [2,1,3]), 3,3,n/3,[])), 3), 1), 3, []).';
pl = reshape(sum(sum(bsxfun(@times, alpha, reshape(permute(reshape(Tij.', n,3,[]), [2,1,3]), 3,3,n/3,[])), 3), 1), 3, []).';
end
function [elements, counts] = intUnique(a)
a = a(:);
if sum((a)~=round(a))>0
error('a must be int!');
end
offsetA = min(a)-1;
a = a-offsetA;
counts = accumarray(a, 1);
elements = find(counts);
counts = counts(elements);
elements = elements+offsetA;
end
>> which fft
built-in (D:\MATLAB\R2008b\toolbox\matlab\datafun\@logical\fft) % logical method
>> class([0 1]) % double
>> class('test') % char
79
82
65
94
...
>> myscore=importdata('score.txt');
>> hist(myscore);
title('$$f(x)=cos(x)$$', 'interpreter','latex' );
>> why
The bald and not excessively bald and not excessively smart hamster obeyed a terrified and not excessively terrified hamster.
>> why
To fool the tall good and smart system manager.
>> why
The rich rich and tall and good system manager suggested it.
>> why
He wanted it that way.
>> why
The programmer suggested it.
>> why
Mara suggested it.
>> why
To please a very terrified and smart and tall engineer.
>> why
The tall system manager obeyed some engineer.
>> why
To satisfy some programmer.
>> why
Mary Ann wanted it that way.
>> why
Can you rephrase that?
>> why
Because Mary Ann wanted it that way.
>> why
How should I know?
>> why
Because they asked the terrified and smart and tall and tall programmer.
>> why
To fool a young tall hamster.
>> why
For the love of a bald and terrified mathematician.
>> why
It's your karma.
>> why
Some terrified and rich system manager knew it was a good idea.
>> why
Don't you have something better to do?
>> why
He suggested it.
>> why
A terrified and good and not very rich engineer helped the bald programmer.
>> why
To fool some kid.
>> why
I obeyed the tall and young system manager.
FFT
为了在科学计算和数字信号处理等领域使用计算机进行Fourier变换,必须将函数定义在离散点上而非连续域内,这一点非常符合所给的波函数的数据,数据点之间同样也存在着定义在离散点上而不是连续域内,且须满足有限性或周期性条件。这种情况下,序列
的离散傅里叶变换为:
FFT(Fast Fourier Transformation),即为快速Fourier变换,是离散Fourier变换的快速算法,它是根据离散Fourier变换的奇偶虚实等特性,对离散Fourier变换的算法进行改进获得的,而Fourier变换则是能将满足一条件的某个函数表示成三角函数(正弦、余弦)或者他们的组合形式。另f(t)为t的周期函数,如果t满足条件:在一个周期内具有有限个极值;绝对可积。则有下式成立。称为积分运算f(t)的Fourier变换。
利用这一方法可以将原先时域的数据改成了频域的图方便后面处理。 这只是我编的MATLAB将DTMF信号的解码程序中的部分程序= =听说FFT用得好几行就没了。。。