Matlab Octave - yszheda/wiki GitHub Wiki

fix

Y = fix(X) 将 X 的每个元素朝零方向四舍五入为最近的整数。对于正方向的 X,fix 的行为与 floor 相同。对于负方向的 X,fix 的行为与 ceil 相同。

Array

repmat

Matrix

filter

   B = A.*(A>0);
   C = A.*(A>0)*100 + A.*(A<0);

unique

squeeze

boolean matrix to numeric

script

pause

directory

cd(fileparts(mfilename('fullpath')))

Figure

legend

 for iN = 1:nN
      legendCell{iN} = num2str(N(iN),'N=%-d');
 end
 legend(legendCell)

color

multiple figures

figure(1)
% plot
figure(2)
% plot
% plot
hold on
% plot

plot

annotations

histogram

h1 = 0.05 * randn(1, 10000);
h2 = 0.10 * randn(1, 10000);
h3 = 0.30 * randn(1, 10000);
[counts1, binCenters1] = hist(h1, 500);
[counts2, binCenters2] = hist(h2, 500);
[counts3, binCenters3] = hist(h3, 500);
plot(binCenters1, counts1, 'r-');
hold on;
plot(binCenters2, counts2, 'g-');
plot(binCenters3, counts3, 'b-');
grid on;
% Put up legend.
legend1 = sprintf('mu = %.3f', mean(h1));
legend2 = sprintf('mu = %.3f', mean(h2));
legend3 = sprintf('mu = %.3f', mean(h3));
legend({legend1, legend2, legend3});

boxplot

axis

tick

figure;
axes;
axis([0 100 0 1000])
set(gca,'XTick',0:20:100);
set(gca,'YTick',0:200:1000);

errorbar

errorbar width

x = 0:pi/3:pi;
y = sin(x);
h=errorbar(x,y,e,'rx:')
hchildren = get(h,'children');
set(hchildren(1), 'LineWidth', 1, 'Color', 'b')
set(hchildren(2), 'LineWidth', 3, 'Color', 'g');
hf = figure;
X = 0:pi/10:pi;
Y = sin(X);
E = std(Y)*ones(size(X));
ha = errorbar(X,Y,E);
hb = get(ha,'children'); 
Xdata = get(hb(2),'Xdata');
temp = 4:3:length(Xdata);
temp(3:3:end) = [];
% xleft and xright contain the indices of the left and right
%  endpoints of the horizontal lines
xleft = temp; xright = temp+1;
% Increase line length by 0.2 units
Xdata(xleft) = Xdata(xleft) - .1;
Xdata(xright) = Xdata(xright) + .1;
set(hb(2),'Xdata',Xdata)

IO

load <file>
print -deps foo.eps
% colorful eps
print -depsc test.eps
print -color -depsc test.eps

save

save('newfile.txt', 'var1, 'var2', ..., '-ascii')

User Interactive Operations

output

input

prompt = 'Do you want more? Y/N [Y]: ';
str = input(prompt,'s');
if isempty(str)
    str = 'Y';
end

escape characters

E.g. escape "'":

xlabel_name = input('Please enter xlabel (default is ''samples''):', 's');

Statistical

self-defined function

default arguments

function f(arg1,arg2,arg3)

  if nargin < 3
    arg3 =   'some default'
  end

end

string

sprintf

⚠️ **GitHub.com Fallback** ⚠️