Magic Traits
文件读取
fid = fopen(fn,'rt');
out = fscanf(fid,spec,inf);
fclose(fid);
2. 读取数据
fid = fopen(fn,'rt');
out = textscan(fid,spec);
运算篇
- fprintf(" xxx %d",a),当a为数组时,会输出数组数目行,每行是一个元素+相关文本的copy
- index: b(i,j,k,:);
- [1,1,5]->[5]: squeeze(a);
- [5]->[1,5]/[5,1]: reshape(a,[1,5]);
- minimal square solution:
% solution1
c=A\b
% solution2
A_=pinv(A)
c=A_*b
- test struct in matlab
% assum data be a struct
disp(['data=',class(data)]);
disp(fieldnames(data));
disp(data);
银河1-睿智的矩阵索引
- 与torch、numpy不同,matlab索引不是先排列最后一维,再排列倒数第二维,
- 反过来先派列第一维,再排列第二维,直到最后一维;
看个例子💐
>> a= rand(2,3,4)
a(:,:,1) =
0.7094 0.2760 0.6551
0.7547 0.6797 0.1626
a(:,:,2) =
0.1190 0.9597 0.5853
0.4984 0.3404 0.2238
a(:,:,3) =
0.7513 0.5060 0.8909
0.2551 0.6991 0.9593
a(:,:,4) =
0.5472 0.1493 0.8407
0.1386 0.2575 0.2543
- 依次排列(2)->(2,3)->(2,3,4)维度
- 所以a(7)在3个维度的下标为(1,1,2)——(2-1)*2*3+(1-1)*2+1,对应0.1190
- 而不是(1,2,3)—(1-1)*2*3+(2-1)*4+3,在numpy中为(0,1,2),
>> a(7)
ans =
0.1190
shortcuts keys
- 查找+替换:ctrl+H
- 反注释:ctrl+T
- cut:ctrl+W