Re: [求救]'.'和'~'的用法

楼主: sppmg (sppmg)   2017-11-04 22:09:22
※ 引述《kazaf1982 (kazaf)》之铭言:
: 在看别人写的程式码 但是有些不解
: 所以想请问板上的前辈
: 第一个问题
: clear all;clc;
: addpath('./code');
: para.img_path = './img_data/single_data/';
: para.result_path = './img_output/';
: 在上面的程式码中,不是很了解'para.'的这个用法
: para是一个特定的用法吗?
这要看你的 para 是什么,不过就两种可能。
1. structure
mantlab doc:
> A structure array is a data type that groups related data
> using data containers called fields. Each field can contain
> any type of data. Access data in a field using dot notation
> of the form structName.fieldName.
简单说就是结构变量含有多个“字段”(field),以“.”存取。各字段可以存放任意
型态的资料,如阵列等等。
定义语法:
s = struct(field1,value1,...,fieldN,valueN)
例:
> s = struct('a', 1, 'b', [1:3])
> s.a
ans = 1
> s.b
ans =
1 2 3
matlab 可以直接用 s.a = 1 来指定,不一定要用 struct 函数。
2. Object
物件为包含自有方法(函数)与属性(变量)的“东西”。
所以你会在程式中看到这种表达方式:
sppmg.name
sppmg.eat(food_a)
前者为 sppmg 物件的属性,用起来就和普通变量一样,可用 = 指定
(前提是这属性被定义成可改)
后者为 eat 方法(函数),用起来和一般函数一样。“()”在 matlab 中可略,
所以在一些背景执行程式中会看到 xxxx.start 这种
(timer 好像可以用 t.start ,但文件是写 start(t) )
详细的定义方式见 class 说明。也可看:
https://www.mathworks.com/help/matlab/matlab_oop/create-a-simple-class.html
: 第二个问题
: [imvector, ~, DisVector]=GetImVector(img, Scale, Scale,0);
: 在输入这边的'~'这个符号代表什么意思?
: 我只知道这是一个NOT的符号
: 但不知道用在output的时候代表什么?
: 谢谢
同推文所说:为了省略。
以 max 函数为例:
mantlab doc:
M = max(A)
[M,I] = max(___)
M -> Maximum values
I -> Index to maximum values
所以如果你只需要 I 而不用 M,为了让程式使用 [M,I] 这种输出格式就必须用 ~ 占位
[~,I] = max(A)
(注:函数中可用 nargout 取得输出参数个数,可依此改变程式运作。)
作者: kazaf1982 (kazaf)   2017-11-10 12:53:00
感恩!

Links booklink

Contact Us: admin [ a t ] ucptt.com