funykatebird的个人博客分享 http://blog.sciencenet.cn/u/funykatebird

博文

常用matlab命令

已有 4093 次阅读 2016-12-17 09:53 |个人分类:问题求解|系统分类:科研笔记

字符处理类

cell char

cell={};初始为空

strcat 字符串拼接

deblank(S) 删除尾部空格

strtrim(S) 删除头尾空格


Matlab - 文件目录路径操作(转)

https://www.cnblogs.com/Jian-thinker/p/7573024.html

分割文件路径

f =D:\\Program Files\\MATLAB\\R2011a\\bin\\test.jpg

>> [pathstr,name,ext]=fileparts(f)

pathstr =D:\\Program Files\\MATLAB\\R2011a\\bin\

name =test

ext =.jpg

matlab---文件路径操作

https://blog.csdn.net/bwcxadzkjd/article/details/73527063



http://blog.sina.com.cn/s/blog_6a388c8f01015n04.html


matlab文件操作fopen,fseek,fread,fclose等

https://blog.csdn.net/huang1024rui/article/details/48440447




算法类

存取中间过程

Temp=[];

Temp=[Temp;[Feature]];

保留变量的小数位数

auc=roundn(auc,-2)%设置两位有效数据


矩阵拼接

水平拼接

a=[1 2 3]

b=[3 4 5]

cat(2,a,b)

竖直拼接

a=[1 2 3]

b=[3 4 5]

cat(1,a,b)

[a,b] [a;b]

矩阵按照某一列排序

函数:sortrows(A,j)


  

判断矩阵中的元素都相等

https://jingyan.baidu.com/article/11c17a2cd121ccf446e39d2b.html


1使用unique函数判断

a=[2 2 2];length(unique(a))

2使用矩阵最大最小值相减判断

max(a)-min(a)

3使用all(~(diff(a)))格式判断

a=[2 2 2];all(~(diff(a))),按回车键,可以得到结果1


去掉矩阵或数组中的重复元素

(1)先sort()排序,再diff()差分,最后用find()找出相同的元素(即元素为0的位置)。该方法较精确。

(2)函数:B = unique(A). 去掉A中重复的元素后返回B. 精度相对较低,但较便利。

 如需要统计相同元素的个数可以参考一下代码

>> a=[1 1 2 3 4 10 10 11 11 13]

a =

     1     1     2     3     4    10    10    11    11    13

>> b=unique(a)

b =

     1     2     3     4    10    11    13

>> c=sum(a.'*ones(1,length(b))==ones(length(a),1)*b,1) 

c =

     2     1     1     1     2     2     1 


 

>> a=[2 3 8 2 4 1 2 3 1];

>> b=unique(a);

>> c(1:length(b))=0;

>> for n=1:length(b)

c(n)=length(find(a==b(n)));

end

>> c

c =

     2     3     2     1     1



如何计算一个向量中每个相同元素的个数


Histogram bin counts 是专门做这个的,可供选择的函数有很多,如果版本比较老,可以用 hist 或 histc,相对新的版本可以用 histcounts

>> histc(x,unique(x))

另外,还可以用 accumarray 实现,accumarray 文档里的例子就是教你怎么做这个的


 x = [1 1 1 2 2 4 5 5 5 5]

diff(find([diff([inf,x])~=0 1]))


假定要分析的矩阵为data,

x=data(:)

x=sort(x);

d=diff([x;max(x)+1]);

count = diff(find([1;d])) ;

y =[x(find(d)) count]

最后输出的y(1)为元素值,y(2)为所对应的元素值在此矩阵中的出现的次数


% 方法好多的。。

% 假如

A = [1 3 5 3 1 5 3 1 1 3 5];

% method1

result=unique(A);

count = hist(A,unique(A));

% method2

A=sort(A);

d=diff([A;max(A)+1]);

count=diff(find([1;d])) ;

y =[A(find(d)) count];

% method3

% 直接用tabulate函数

tabulate(A)



在matlab中怎样找到相同的行?并找到其位置

ismember

https://blog.csdn.net/loveaborn/article/details/8486010

如何使用matlab 将一个数组中重复出现的所有元素剔除

https://blog.csdn.net/zhyh1435589631/article/details/52760523


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% date          : 20161008

% version       : 1.0

% author        : zhyh2010

% description   : remove all the duplicate elements in a set

%                 for example:   [1,1,2,3,4]  ==> [2,3,4]

% input         : array to remove the duplicate elements

% ouput         : array already be removed the duplicated elements

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [output] = removeDuplicate(input)

    simple = unique(input);

    count = histc(input, simple);

    ids = find(count == 1);

    output = simple(ids);

end


matlab如何剔除无用数据

https://blog.csdn.net/sinat_20265495/article/details/51264320

z=find(~isnan(A))

B=A(z)

matlab中去掉矩阵重复行并且不改变原顺序

https://blog.csdn.net/Cheese_pop/article/details/58157735

https://blog.csdn.net/qinze5857/article/details/80108909



function new_a = unique_arrys(a)

    [b,location] = unique(a,'rows','first');

    res = sortrows([location,b]);

    new_a=res(:,2:size(res,2));


Matlab快速删除矩阵中满足条件的行、列的方法

https://blog.csdn.net/qysh123/article/details/9036569

  1. id=A(:,2)>0 & A(:,2)<5;

  2. A(id,:)=[];

Matlab去除矩阵内的0元素

https://blog.csdn.net/SailingLT/article/details/80673556

a=[1,0,2,3,0,4,5]

a(find(a==0))=[]



MATLAB如何判断结构体为空

s=struct

s = 

1x1 struct array with no fields.

>> whos s

Name Size Bytes Class Attributes

s 1x1 0 struct 

>> f=isstruct(s)

f =

1

>> ff=exist('s.field')

如果采用exist就算结构体有值,exist也是等于0

采用isfield,就可解决了!
isfield returns false if the field or fieldnames input is empty.

isstruct判断输入是否为结构体
Determine whether input is structure array
isempty判断输入数组是否为空
Determine whether array is empty




' 转置或引用
=  赋值
= =  相等
~= 不相等
< ,>,<=,>=  关系操作符

&  逻辑与

|  逻辑或

~  逻辑非

MATLAB常用函数表(1)

http://blog.sina.com.cn/s/blog_6291aec80100fs52.html


绘图类

plot中画线的颜色通常是八种:

标记符    颜色
r          红
g          绿
b          蓝
c          蓝绿
m          紫红
y          黄
k          黑
w          白


clf; 用来清除图形的命令。一般在画图之前用。

close all %关闭所有图像窗口
close(figure(gcf)) %关闭当前激活图像窗口


imresize的用法
ai=imread('H:a1.jpg');    %ai大小为381x376
bi=imresize(ai,0.6);        %bi缩放为ai的0.6倍
figure
imshow(bi)
ci=imresize(ai,[256,256]);         %把ai转成256x256的大小
figure
imshow(ci)


matlab的图形窗口每次背景都是灰色的,而我希望每次都是白色的背景,方便用图;

每次总是需要添加figure('color','w');或者figure('color',[1 1 1])或者set(gcf,'color','w');很不方便。

正确用法:

matlab命令框里面输入  set(0,'defaultfigurecolor','w')


plot 对应点显示数值

clear
clc
x=[1 2 3 4 5];
y=[3 6 10 16 20];
str=[repmat('  X:',5,1) num2str(x') repmat(', Y:',5,1) num2str(y')];
plot(x,y,'-o')

text(x,y,cellstr(str)

直方图上显示数据

  1. x = rand(100,1);

  2. [n,y] = hist(x);

  3. bar(y,n);

  4. for i = 1:length(y)

  5. text(y(i),n(i)+0.5,num2str(n(i)));

  6. end


plot 绘图

不显示上面和右侧的边框命令:box off



matlab绘图-图例位置与线型选择局部放大图中图图片大小

http://blog.sciencenet.cn/home.php?mod=space&uid=597740&do=blog&quickforward=1&id=836882


http://blog.sina.com.cn/s/blog_4c0cb1c00100x8bn.html


matlab figure 图像去除白边问题

http://blog.sciencenet.cn/blog-597740-1001489.html

Matlab中对图像应用plot或者rectangle后的图像保存问题

http://blog.sciencenet.cn/blog-597740-1004598.html



定位图像中像素坐标的方法(matlab)

https://blog.csdn.net/afgh2587849/article/details/6052586


1. ginput

2. impixelinfo(更为方便)


判断图像类型:Isgray(判断是否灰度图),Isbw(二进制图),Isind(索引图),Isrgb(RGB图)

https://blog.csdn.net/junshen1314/article/details/19411593?locationNum=7&fps=1


数学形态学

https://wenku.baidu.com/view/eec1feec5ef7ba0d4a733b50.html


matlab实现形态学处理


https://blog.csdn.net/qq_26460507/article/details/61914657


图像边缘检测经典算子及MATLAB实现

https://blog.csdn.net/u012808193/article/details/45722283




https://blog.sciencenet.cn/blog-597740-1021269.html

上一篇:聚类分析
下一篇:vision绘图问题
收藏 IP: 36.102.228.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 05:21

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部