Matlab code performance;Part I: Techniques for Improving Performance;Use the appropriate preallocation function for the kind of array you want to initialize:
zeros for numeric arrays
cell for character arrays
Preallocating a Nondouble Matrix
A = int8(zeros(100));
A = zeros(100, int8);
;Assigning Variables
If you need to store data of a different type, it is advisable to create a new variable. Changing the class or array shape of an existing variable slows MATLAB down because it takes extra time to process.
You should not assign a real value to a variable that already holds a complex value, and vice versa. Assigning a complex number to a variable that already holds a real number impacts the performance of your program.
X = 23;
.
-- other code --
.
X = A; % X changed from type double to char
.
-- other code --;Using Appropriate Logical Operators
Operator Description
, | Perform logical AND and OR on arrays element by element
, || Perform logical AND and OR on scalar values with short-circuiting
In if and while statements, it is more efficient to use the short-circuit operators, and ||:
if (isnumeric(varargin{1})) (ischar(varargin{2}));Additional Tips on Improving Performance;Part II: Vectorization;Using Vectorization;Indexing Methods for Vectorization;Linear Indexing
A = [11 12 13; 14 15 16; 17 18 19];
A(6)
A([3,1,8])
A([3;1;8])
Note: Use the functions sub2ind and ind2sub to convert between subscripted and linear indices.;Logical Indexing
MATLAB selects elements of A that contain a 1 in the corresponding position of the logical matrix:
A = [11 12 13; 14 15 16; 17 18 19];
A(logical([0 0 1; 0 1 0; 1 1 1]))
;Array Operations;Logical Array Operations;Matrix Operations;Ordering, Setting, and Counting Operations;Functions Commonly Used in Vectorizing;Part III Matrix Indexing in MATLAB;Indexing Matrices with Two Subscripts
A = magic(4)
A(2,4) % Extract the element in row 2, column 4
A(2:4,1:2)
A(3
您可能关注的文档
- 扬州一日游攻略个人手打BYTY.doc
- 扬州市规划展览馆暨市民中心2.pptx
- 托管2016年3月31日医院感染相关知识培训.ppt
- 扑克牌做收纳盒.ppt
- 扬州市邗江区旅游资源调研.pptx
- 技术图纸更改管理制度2015-3.docx
- 技术员上门服务标准.ppt
- 扬州慢教学设计.ppt
- 技术培训模板----记录与统计报表.ppt
- 扬州慢公开课.ppt
- 2025-2026 学年统编版七年级上册语文第一单元测试卷(附答案).docx
- 2025-2026 学年人教版八年级下册历史第七单元测试卷(附答案).docx
- 2025-2026 学年人教版九年级上册道德与法治第九单元测试卷(附答案).docx
- 2025-2026 学年人教版九年级上册英语第三单元测试卷(附答案).docx
- 2025-2026 学年人教版七年级上册英语第七单元测试卷(附答案).docx
- 2025-2026 学年人教版七年级上册语文第一单元测试卷(附答案).docx
- 2025-2026 学年人教版七年级下册英语第八单元测试卷(附答案).docx
- 2025-2026 学年统编版八年级上册数学第二单元测试卷(附答案).docx
- 2025-2026 学年统编版八年级下册道德与法治第九单元测试卷(附答案).docx
- 2025-2026 学年上学期高一数学期末综合评估试卷(附答案).docx
最近下载
- 天津市部分区2024-2025学年八年级上学期11月期中数学试题(解析版).docx VIP
- 会议活动策划方案模板.docx VIP
- 土方意向协议书范本.docx VIP
- 履带吊安拆监理细则.docx VIP
- T_ZZB 2614-2022 BD100生物柴油标准.docx VIP
- 2026领导班子成员民主生活会“五个带头”个人对照检查材料8篇.docx VIP
- 2025年度民主生活会个人对照检查发言材料.docx VIP
- 山东省烟台龙口市(五四制)2022-2023学年六年级上学期期末考试数学试题(含答案).docx VIP
- 部编版七年级语文上册第三单元第9课时_主题阅读:有为有不为.docx VIP
- 《人工智能辅助的翻译技术》课件.ppt VIP
原创力文档

文档评论(0)