MATLAB Tricks (Updating)
来源:互联网 发布:常州seo排名公司 编辑:程序博客网 时间:2024/06/05 16:45
Memo for MATLAB Tricks.
T1. Read images by for-loop
num_img = 10; % number of images in each foldernum_fdr = 40; % number of foldersnum_ft = 50; % number of featuressizer = 40; % standard row size of resized imagesizec = 40; % standard column size of resized imagefilepathsrc = 'A/B'; % file path sourcefor i=1:num_fdr folderpathsrc = sprintf('%s%d',filepathsrc,i); for j=1:num_img % input the jth image in ith folder filename=sprintf('%s%s%d%s',folderpathsrc,'/',j,'.pgm'); % resize original images img = imresize(imread(filename), [sizer sizec], 'bilinear'); % input data into workspace eval([sprintf('%s%d%s%d','f',i,'_',j) '=img;']);end
T2. Execute loop iterations in parallel - Parfor
T3. roots
% determine the weight lamdasyms wF = (1-w) * F1 + w * F2;coef = sym2poly(det(F));lamda = roots(coef);
T4. bsxfun
Try to substitute bsxfun
for for
:
C = bsxfun(fun,A,B) appliesthe element-by-element binary operation specified by the function handle fun to arrays A and B,with singleton expansion enabled.fun can be one of the following built-in functions: @plus Plus @minus Minus @times Array multiply @rdivide Right array divide @ldivide Left array divide @power Array power @max Binary maximum @min Binary minimum @rem Remainder after division @mod Modulus after division @atan2 Four quadrant inverse tangent @hypot Square root of sum of squares @eq Equal @ne Not equal @lt Less than @le Less than or equal to @gt Greater than @ge Greater than or equal to @and Element-wise logical AND @or Element-wise logical OR @xor Logical exclusive OR
Miscellaneous
M1. Matlab Tricks
- 定位矩阵的全零行:
find(sum(abs(A),2)==0)
- 结果保存到文本中:
save result.txt p -ascii
- 把矩阵中的某些元素值为a的全部替换为b:
C(C==a)=b
- 无界面情况下如何绘图并保存:
set(gca, ‘UserData’, {‘gscatter’ x y g}); saveas(gca, ['D:/test.fig'])
- 在程序开头加上
rand(‘state’,1)
;randn(‘state’,1)
; 以保证每次生成的随机数一致。
M2. Matlab下大矩阵运算
- 内存
memory
: 查看系统当前内存使用情况whos
: 查看每个变量所占用的内存
- 速度
tic
,toc
: 记录每段代码的执行时间。profile
: 查看每条语句的详细运行情况。先运行profile on
,再运行需要测试的代码,然后使用profile viewer
来查看报告。
sparse
- 当一个矩阵中有大量的0时就一定要采用sparse型进行运算,不只大大减少内存会消耗,而且时间消耗也会大大减少。
- 注意,一般Sparse矩阵运算如sum, diag等之后还是sparse矩阵,而当sparse矩阵和full矩阵一起参加运算时,结果会被强制转为full矩阵。
- Sparse矩阵在指定下标的循环运算中不占优势。
- 注意,当一个矩阵并不是很稀疏时尽量不要采用sparse方式存储,不便于矩阵运算,对于一些矩阵操作,如求矩阵转置等操作会变得异常异常的慢!
阅读全文
0 0
- MATLAB Tricks (Updating)
- LaTex Tricks (Updating)
- Matlab tricks
- Matlab: Tips and tricks
- Matlab Coding Tricks
- Matlab有趣源码(Updating...)
- Matlab入门基础详解【updating】
- [Matlab]科研中的心得(updating)
- MATLAB”奇淫技巧“(Updating。。)
- tricks
- tricks
- Matlab Tricks(一)—— figure(1)
- Matlab Tricks(三)—— subplot 的组织
- Matlab Tricks(四)—— remove DC
- Matlab Tricks(五)—— shuffle 一个矩阵
- Matlab Tricks(六)—— 矩阵乘法的实现
- Matlab Tricks(十)—— padarray 的实现
- Matlab Tricks(十一)—— 向量的逆序输出
- 第四讲 身份认证技术
- vuejs中created与mounted区别
- LaTex Beamer中插入视频
- 171223 逆向-JarvisOJ(EvilExe)
- MATLAB 并行for运算
- MATLAB Tricks (Updating)
- LaTex Tricks (Updating)
- CentOS 7.3+PHP7.2+mysql5.7+Workerman3安装
- Android中MVP模式的创建P的注解工厂,代理模式绑定和V的生命周期以恢复数据(四)
- 使用valgrind的callgrind工具进行多线程性能分析
- 在PyQt5中的菜单栏和工具栏
- 各大公司Java后端开发面试题总结(2017更新)-慢慢看
- norflash芯片内执行(XIP)
- 1224的坑