matconvnet用到的 matlab函数

目录

1.dir()
2.for循环
3.ismember()

1.dir()

dir函数的作用:返回文件夹中的所有文件或者文件夹所组成的列表

dir  %returns a list of files and folders in the current folder.

dir name   %returns a list of files and folders that match the string name. When name is a folder, dir lists the contents of the folder.      Specify name using absolute or relative path names,当name是一个文件夹时,返回文件夹中所有内容

listing = dir(name)    % returns attributes about name.返回name的属性,是一系列结构体类型的数组。

例如>>a=dir('my_dir')

61x1 struct array with fields:

    name
    date
    bytes
    isdir
    datenum 

例,我们想知道该文件夹下第三个文件的名字,即就是访问该结构体中的name成员

>>a.name(3)

ans =

             xxx.jpg  %假设是文件名为xxx.jpg

该函数的输入类型是字符串类型string,输出为结构体数组

具体细节可在matalb中输入>>doc dir


2.for循环

for循环是一种重复控制结构,可以让您有效地编写一个需要执行特定次数的循环。

语法

MATLAB中for循环的语法是 -

for index = values
   <program statements>
            ...
end

 
 
MATLAB

值(values)具有以下格式 -

值格式描述
initval:endvalindex变量从initvalendval每次递增1,并重复程序语句的执行,直到index大于endval
initval:step:endval通过每次迭代值步长(step)增加索引(index)的值,或者当step为负时递减。
valArray在每个迭代中从数组valArray的后续列创建列向量索引。 例如,在第一次迭代中,index = valArray(:,1)。 循环最多执行n次,其中n是由numel(valArray,1,:)给出的valArray的列数。valArray可以是任何MATLAB数据类型,包括字符串,单元格数组或结构体。

示例 - 1

创建脚本文件并键入以下代码 -

for a = 10:20 
   fprintf('value of a: %d\n', a);
end

 
 
MATLAB

运行示例代码时,会显示以下结果 -

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20

 
 
Shell

示例 - 2

创建脚本文件并键入以下代码 -

for a = 1.0: -0.1: 0.0
   disp(a)
end

 
 
MATLAB

运行示例代码时,会显示以下结果 -

 1
 0.90000
 0.80000
 0.70000
 0.60000
 0.50000
 0.40000
 0.30000
 0.20000
 0.10000
0

 
 
Shell

示例 - 3

创建脚本文件并键入以下代码 -

for a = [24,18,17,23,28]
   disp(a)
end

 
 
MATLAB

运行示例代码时,会显示以下结果 -

24

18

17

23

28

 
 
Shell
比如我遇到的代码段:imagenet自带的

% -------------------------------------------------------------------------
%                                                           Training images
% -------------------------------------------------------------------------

fprintf('searching training images ...\n') ;
names = {} ;
labels = {} ;
for d = dir(fullfile(opts.dataDir, 'images', 'train', 'n*'))'
  [~,lab] = ismember(d.name, cats) ;
  ims = dir(fullfile(opts.dataDir, 'images', 'train', d.name, '*.JPEG')) ;
  names{end+1} = strcat([d.name, filesep], {ims.name}) ;
  labels{end+1} = ones(1, numel(ims)) * lab ;
  fprintf('.') ;
  if mod(numel(names), 50) == 0, fprintf('\n') ; end
  %fprintf('found %s with %d images\n', d.name, numel(ims)) ;
end
names = horzcat(names{:}) ;
labels = horzcat(labels{:}) ;

if numel(names) ~= 1281167
  warning('Found %d training images instead of 1,281,167. Dropping training set.', numel(names)) ;
  names = {} ;
  labels =[] ;
end

names = strcat(['train' filesep], names) ;

imdb.images.id = 1:numel(names) ;
imdb.images.name = names ;
imdb.images.set = ones(1, numel(names)) ;
imdb.images.label = labels ;

3.ismember()

http://blog.csdn.net/zlk961543260/article/details/70240714

 今天在MATLAB中遇见了ismember函数,就懒了上网上搜了一下是怎么回事,找了半天也没有说明白的,后来还是自己在MATLAB中help一下,看看这个函数到底干了啥!

用法:(举例)

[plain]  view plain  copy
  1. a=[1 2 3 4 5];  
  2. b=[3 4 5 6 7];  
  3. c=[2 4 6 8 10];  
  4. ismember(a,b)  
  5. [lia,lib]=ismember(a,c)  

直接复制代码到MATLAB运行窗口,可以看到结果如下:

ans =

     0     0     1     1     1
lia =
     0     1     0     1     0
lib =
     0     1     0     2     0

       那么结果是什么意思呢,这个函数主要是看矩阵a中的数是不是矩阵b中的成员,是的话结果返回1,不是返回0;这就是ismember(a,b)结果为什么是0     0     1     1     1,这个不用再多解释了吧!(这个结果是基于矩阵a的,意思是结果为什么是0     0     1     1     1,而不是  1     1     1   0   0

       第二种用法,就是返回值是两个矩阵,第一个矩阵跟上面的结果一样,因此lia =   0     1     0     1     0;第二个矩阵大家也许猜到了,第一个矩阵中的元素是第二个矩阵的成员的时候,看看这个值在第二个矩阵中的索引,也就是说a中2,4是c中的成员,2,4在c中的索引也就是下标为1,2。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值