Octave

% comment
~= not equal
PS1(‘>> ”);
; suppress the print output
a ——a=?
disp(a) ——value of a
disp(sprintf(‘2 decimals: %0.2f’,a))
——2 decimals: 3.14
format long
a
——a=??
matrix or vector:
A=[1 2;3 4;5 6]
ones(2,3)
zeros(1,3)
2*ones(2,3)
rand(1,3)
randn(1,3)
eye(3)
vector:
v=1:6
v=1:0.1:2
if w=-6+sqrt(10)*(randn(1,10000))
jist(w)

help eye (Q to quit)
help rand

size(A) %return the size of A(which is also a matrix)
size(A,1) %return the number of rows
size(A,2) %…………………………………….columns
length(v)
e.g.length([1 2 3 4])
length([1;2;3;4])
length(A) %return the size of the longest dimension

pwd %show the current directory
cd %change directory
ls %list the directories

to load data and find data on the file system
load featuresX.dat
load(‘featureX.dat’)

who %shows the variables in Octave workspace
whos %shows the details as well
clear featuresX %delete certain variable
clear %delete all variables

save data
v=priceY(1:10) %sets v to be the first 10 elements of vector priceY
save hello.mat v; %save to file
save hello.txt v -ascii %save as text(ASCII)

A(3,2) %fetch certain element with the index
A(2,:) %fetch everything in the second row
A(:,2) %…………………………………………………….column
A(2:end)
A([1 3],:)%………………………whose first indexes one or three
A(:,2)=[10;11;12]
C=[AB]
A=[A,[100;101;102]]; %append another column vector to the right
[A B]==[A,B]
C=[A;B]
A(:) %put all the elements of A into a single vector

find(A==2);
[m,n]=find(A,k,2);

compute

A*B
A.*B
A.^2
1./A  %a clue that this is an elements wise operation
log(A)
exp(A)
abs(A)
-A
v+ones(length(v),1)==v+1
A' %transpose
max(A) %does the column wise maximum
[val,ind]=max(a) %val and index which has the maximum value of A
a<3 %elements wise
find(a<3)
A=magic(3)
[r,c]=find(A>=7)
sum(a)
prod(a)
floor(a)
ceil(a)
max(rand(3),rand(3))
max(A,[],1)
max(A,[],2)
max(A) %default as column wise
max(max(A))/max(A(:)) %entire max
sum(sum(A.*eye(9))) %sum the diagonal elements of a 9*9 matrix
sum(sum(A.*flipud(eye(9))))
pinv(A)

plotting data

t=[0:0.01:0.98];
y1=sin(2*pi*4*t);
plot(t,y1,'r'); %horizontal axis is t,with red color
hold on; %plot new figure on the top of old one
figure(1);plot(t,y1);

subplot(1,2,1); %divides plot a 1*2 grid,access first element
plot(t,y1);
subplot(1,2,2);
plot(t,y2);

axis([0.5 1,-1 1])
xlabel('time') %label the horizontal axis
legend('sin','cos')
title('my plot')
cd 'C:\';print -dpng 'myPlot.png'
imagesc(A),colorbar,colormap
help plot
help axis
close

control statement

for-loop

v=zeros(10,1);
for i=1:10,v(i)=2^i;end;
indices=1:10;
for i=indices,disp(i);end;
%traversal elements of certain matrix one by one

while-loop

i=1;
while i<=5,v(i)=100;i=i+1;end;
i=1;
while true,
    v(i)=999;
    i=i+1;
    if i==6,
      break;
      end;
end;

if-else

  if..,
      ;
  elseif..,
      ;
  else..
      ;
  end;
exit;
quit;

use wordpad(not notepad) to open the .m

function
definition(in a file)

function y=F(x)    
%save the value to y and return y,with one argument x
y=x^2;%function body
function [y1,y2]=F2(x)
y1=x^2;
y2=x^3;%return multiple values

call function

cd '..'
F(5)
addpath('..')
F(5)
[a,b]=F2(5);

vectorize

这里写图片描述

a = 1:10; % Create a and b
b = 3;
a == b % You should try different values of b here

read images

% Load 128x128 color image (bird small.png) 
A = imread('bird small.png');
% You will need to have installed the image package to used % imread. If you do not have the image package installed, you % should instead change the following line to % % load('bird small.mat'); % Loads the image into the variable A
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值