当前位置:网站首页>Matlab for circular pit
Matlab for circular pit
2022-07-25 02:39:00 【HackerTom】
matlab use for Nested loop traversal array , There may be bug.matlab/octave Environmental Science :
- linux:Matlab R2018a [1]
- windows:GNU Octave, version 5.2.0
With for x = vector Form traversal , There are several cases :
- Traverse row vector elements ,OK;
- Traverse column vector elements ,OK;
- nesting : Layman column , Something happened : The outer traversal line is normal , The inner traversal column is assigned to the whole column every time ;
- nesting : Outside column inside line , Something happened : The outer traversal column does not involve nesting at first for It's normal , Getting columns in the inner loop is the whole column ; The inner traversal line is normal ;
- nesting : Layman and expert ,OK;
- nesting : Outer column inner column , Something happened : The outer traversal column does not involve nesting at first for It's normal , Getting columns in the inner loop is the whole column ; And the inner traversal column is also the whole column ;
See the code for details. 、 Comments and output :
clear all; clc;
a = [1 2 3]; % Row vector
fprintf("a size: [%s]\n", num2str(size(a)));
fprintf("single loop a\n");
for i = a
fprintf("i: %d\n", i); % OK
end
b = [4; 5; 6]; % Column vector
fprintf("b size: [%s]\n", num2str(size(b)));
fprintf("single loop b\n");
for i = b
fprintf("i: %d\n", i); % OK
end
fprintf("nested loop: row->col\n");
for ia = a
fprintf("--- a: %d\n", ia); % OK
for ib = b
fprintf("a: %s, b: [%s], %s\n", num2str(ia), num2str(size(ib)), num2str(ib)); % ia OK,ib Entire column
end
end
fprintf("nested loop: col->row\n");
for ib = b
fprintf("--- b: %d\n", ib); % OK
for ia = a
fprintf("b: %s, a: [%s], %s\n", num2str(ib), num2str(size(ia)), num2str(ia)); % ib Entire column ,ia OK
end
end
fprintf("nested loop: row->row\n");
for ia = a
fprintf("--- a: %d\n", ia); % OK
for ia2 = a
fprintf("a: %s, a2: [%s], %s\n", num2str(ia), num2str(size(ia2)), num2str(ia2)); % OK
end
end
fprintf("nested loop: col->col\n");
for ib = b
fprintf("--- b: %d\n", ib); % OK
for ib2 = b
fprintf("b: %s, b2: [%s], %s\n", num2str(ib), num2str(size(ib2)), num2str(ib2)); % ib、ib2 It's the whole column
end
end
fprintf("nested loop: row->row->row\n");
c = [7, 8, 9];
for ia = a
fprintf("a: [%s], %d\n", num2str(size(ia)), ia); % OK
for ib = b' % Change to line
fprintf("|- b: [%s], %d\n", num2str(size(ib)), ib); % OK
for ic = c
fprintf("|- |- c: [%s], %d\n", num2str(size(ic)), ic); % OK
end
end
end
- Output
a size: [1 3]
single loop a
i: 1
i: 2
i: 3
b size: [3 1]
single loop b
i: 4
i: 5
i: 6
nested loop: row->col
--- a: 1
a: 1, b: [3 1], 456
--- a: 2
a: 2, b: [3 1], 456
--- a: 3
a: 3, b: [3 1], 456
nested loop: col->row
--- b: 4
--- b: 5
--- b: 6
b: 456, a: [1 1], 1
b: 456, a: [1 1], 2
b: 456, a: [1 1], 3
nested loop: row->row
--- a: 1
a: 1, a2: [1 1], 1
a: 1, a2: [1 1], 2
a: 1, a2: [1 1], 3
--- a: 2
a: 2, a2: [1 1], 1
a: 2, a2: [1 1], 2
a: 2, a2: [1 1], 3
--- a: 3
a: 3, a2: [1 1], 1
a: 3, a2: [1 1], 2
a: 3, a2: [1 1], 3
nested loop: col->col
--- b: 4
--- b: 5
--- b: 6
b: 456, b2: [3 1], 456
nested loop: row->row->row
a: [1 1], 1
|- a: 1, b: [1 1], 4
|- |- a: 1, b: 4, c: [1 1], 7
|- |- a: 1, b: 4, c: [1 1], 8
|- |- a: 1, b: 4, c: [1 1], 9
|- a: 1, b: [1 1], 5
|- |- a: 1, b: 5, c: [1 1], 7
|- |- a: 1, b: 5, c: [1 1], 8
|- |- a: 1, b: 5, c: [1 1], 9
|- a: 1, b: [1 1], 6
|- |- a: 1, b: 6, c: [1 1], 7
|- |- a: 1, b: 6, c: [1 1], 8
|- |- a: 1, b: 6, c: [1 1], 9
a: [1 1], 2
|- a: 2, b: [1 1], 4
|- |- a: 2, b: 4, c: [1 1], 7
|- |- a: 2, b: 4, c: [1 1], 8
|- |- a: 2, b: 4, c: [1 1], 9
|- a: 2, b: [1 1], 5
|- |- a: 2, b: 5, c: [1 1], 7
|- |- a: 2, b: 5, c: [1 1], 8
|- |- a: 2, b: 5, c: [1 1], 9
|- a: 2, b: [1 1], 6
|- |- a: 2, b: 6, c: [1 1], 7
|- |- a: 2, b: 6, c: [1 1], 8
|- |- a: 2, b: 6, c: [1 1], 9
a: [1 1], 3
|- a: 3, b: [1 1], 4
|- |- a: 3, b: 4, c: [1 1], 7
|- |- a: 3, b: 4, c: [1 1], 8
|- |- a: 3, b: 4, c: [1 1], 9
|- a: 3, b: [1 1], 5
|- |- a: 3, b: 5, c: [1 1], 7
|- |- a: 3, b: 5, c: [1 1], 8
|- |- a: 3, b: 5, c: [1 1], 9
|- a: 3, b: [1 1], 6
|- |- a: 3, b: 6, c: [1 1], 7
|- |- a: 3, b: 6, c: [1 1], 8
|- |- a: 3, b: 6, c: [1 1], 9
It is more stable to traverse by subscript , But when traversing a matrix 、 When higher-order tensors are used, attention should be paid to adding : Means take all ( see [2]):
clear all; clc;
a = [1 2 3]; % That's ok
b = [4; 5; 6]; % Column
for ia = 1 : length(a) % Subscript
xa = a(ia);
for ib = 1 : length(b) % Subscript
xb = b(ib);
fprintf("a: %d, b: %d\n", xa, xb); % OK
end
end
c = reshape(1 : 12, 3, 4); % matrix
for ic = 1 : size(c, 1) % Subscript
d = c(ic); % The second dimension is not added `:`, Something happened : You can only get the first column
e = c(ic, :); % The second Vega `:`,OK
fprintf("d: %d\n", d);
fprintf("e:"), disp(e);
end
- Output
a: 1, b: 4
a: 1, b: 5
a: 1, b: 6
a: 2, b: 4
a: 2, b: 5
a: 2, b: 6
a: 3, b: 4
a: 3, b: 5
a: 3, b: 6
d: 1
e: 1 4 7 10
d: 2
e: 2 5 8 11
d: 3
e: 3 6 9 12
References
边栏推荐
- Details of C language compilation preprocessing and comparison of macros and functions
- Simulate the implementation of strstr
- "Introduction to interface testing" punch in to learn day07: websocket interface: how to test a completely unfamiliar protocol interface?
- Tp5.1 include include files (reference public files)
- QT realizes calendar beautification
- Tp5.1 paging (with parameter transfer)
- Scalar, vector, matrix calculus
- Go language path is relative, but relative Import paths are not supported in module mode
- Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
- JS utils tool function that makes you get twice the result with half the effort
猜你喜欢

Sequence diagram of UML diagram series

What are you working for? Don't give up is our only choice, come on, everyone

Yunyuanyuan (VIII) | Devops in depth Devops

My creation anniversary (3rd Anniversary)

Tp5.1 login configuration method of whether to login public functions (complete login case)

Use unicloud cloud function to decode wechat motion steps in applet

Picgo configuring Alibaba cloud OSS

On Calc optimization of calcite

Project management tool Zen

Coal industry supply chain centralized mining system: digitalization to promote the transformation and upgrading of coal industry
随机推荐
In the post deep learning era, where is the recommendation system going?
On the economic model of open source ecology
Keepalivetime=0 description of ThreadPoolExecutor
Detailed explanation of the principles and differences between static pages and dynamic pages
These 11 chrome artifacts are extremely cool to use
High performance memory recovery technology for decrypting ark -- HPP GC
Generator set work arrangement problem code
YouTube Download and (batch) Download
Class notes (4) (2) -- 572. Compete
Jedispoolconfig parameter configuration from the perspective of source code
[TinyML]EfficientFormer:Vision Transformers at MobileNet Speed
Babbitt | metauniverse daily must read: Dubai launched the national metauniverse strategy, which plans to increase the number of related companies of metauniverse by five times in the next five years
QT realizes calendar beautification
Today in history: the kotlin language was published for the first time; The father of the IMAP agreement was born; CT imaging achieves a new breakthrough
I was forced to graduate by a big factory and recited the eight part essay in a two-month window. Fortunately, I went ashore, otherwise I wouldn't be able to repay the mortgage
Simulation Implementation of [STL] string class
When executing SQL query statements in MySQL database, the underlying implementation principle (ultra detailed)
Work arrangement of generator set
Summary thinking caused by the function of a SMS verification code [easy to understand]
Detailed explanation of MySQL, Oracle and PostgreSQL database index failure scenarios