u = 0:(M-1) 在 Matlab 中做了什么,我们如何在 Python 中做同样的事情?
以下是您原始 Matlab 代码的摘录:
% BEGIN MATLAB %
u = 0:(M-1);
% END MATLAB %
代码有什么作用?
假设M = 7。那么Matlab代码就简化了:
u = 0:6;
结果是一个数组u,从0开始,到6结束:
u = [0 1 2 3 4 5 6]
本质上,您正在初始化一个连续整数数组。
在Python中有多种方法可以完成类似的事情:
# Begin Python
M = 7
u = list(range(0, M))
# End python
请注意,range(0, 7) 看起来像 [0, ..., 5, 6],而不是 [0, ..., 6, 7]
Python的range函数自动从上限中减去1
如果你真的在做 Matlab 类型的东西,那么 numpy 是你想在 Python 中使用的库:
import numpy as np
u = np.array(range(0, 7))
从 0 开始的索引与从 1 开始的索引
请注意,Matlab 索引从 1 开始。
Python 索引从 @987654338 开始@.
ARRAY = ["red", "blue", "white", "green"]
+--------------+-------+--------+---------+---------+
| ARRAY | "red" | "blue" | "white" | "green" |
+--------------+-------+--------+---------+---------+
| PYTHON INDEX | 0 | 1 | 2 | 3 |
| MATLAB INDEX | 1 | 2 | 3 | 4 |
+--------------+-------+--------+---------+---------+
了解find 函数
将find从Matlab翻译成英文
考虑 Matlab 中的 find 函数:
idx = find(u > M/2); % this is matlab-code
函数调用find(u) 将在整个数组u 中搜索严格大于M/2 的任何内容。然后find(u) 将返回大于M/2 的所有索引的列表
考虑以下find 函数的示例:
u = [98 00 00 87 49 50 51 00 85];
% 1 2 3 4 5 6 7 8 9 .....ARRAY INDICIES
idx = find(u > 50);
disp(idx)
% displays .... 1 4 7 9
find(u > 50) 将查找u 的每个元素的索引大于或等于51
考虑代码u(idx) = 22;
我们有以下结果:
+---------------------+------+-----+-----+------+-----+-----+------+-----+------+
| MATLAB INDICIES | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---------------------+------+-----+-----+------+-----+-----+------+-----+------+
| print(u) | 99 | 00 | 00 | 99 | 49 | 50 | 51 | 00 | 99 |
+---------------------+------+-----+-----+------+-----+-----+------+-----+------+
| % u > 50? | %yes | %no | %no | %yes | %no | %no | %yes | %no | %yes |
+---------------------+------+-----+-----+------+-----+-----+------+-----+------+
| idx = find(u > 50); | | | | | | | | | |
| u(idx) = 22; | | | | | | | | | |
+---------------------+------+-----+-----+------+-----+-----+------+-----+------+
| print(u) | 22 | 0 | 0 | 22 | 49 | 50 | 22 | 0 | 22 |
+---------------------+------+-----+-----+------+-----+-----+------+-----+------+
数组u 中大于或等于51 的所有内容都替换为22
将find 从英语翻译成Python
假设您在 Python 中有一个数组 u。
你想用22替换每个大于或等于51的整数
您可以在 Python 中使用 numpy 库来做到这一点:
# This is Python (not matlab)
import numpy as np
u = [98 00 00 87 49 50 51 00 85];
u = np.array(u)
u[u > 50] = 22
# THIS IS PYTHON CODE (not matlab)
注意u[u > 50] = 22和下面的一样:
# THIS IS PYTHON CODE (not matlab)
indicies = type(u).__gt__(u, 50)
u.__setitem__(indicies, 22)
# THIS IS PYTHON CODE (not matlab)
将 find 从 Matlab 转换为 Python
如果您将部分原始代码从 Matlab 转换为 Python,它将如下所示:
MATLAB 输入:
M = 7
u = 0:(M-1);
idx = find(u > M/2);
u(idx) = u(idx) - M;
PYTHON 输出:
# THIS IS PYTHON CODE (not matlab)
import numpy as np
M = 7
u = np.array(range(0, M))
idx = u > M/2
u[idx] = u[idx] - M
# THIS IS PYTHON CODE (not matlab)
将Matlab代码的所有ALL翻译成英文 和 数学
在文章的开头,我解释了您的 Matlab 代码的几个单独部分的作用。
现在,让我们将整个 Matlab 脚本翻译成英语和数学。
***下面有一些与您的原始/旧 MATLAB 相似的东西 ***
function u = GenerateArray(M)
u = 0:(M-1);
idx = find(u > M/2);
u(idx) = u(idx) - M;
end
M = 7;
u = GenerateArray(M);
N = 9;
v = GenerateArray(N);
*** 表格中的行为***
我认为 Matlab 代码作为表格比作为代码更容易理解:
+--------------+---------------------------+
| WHOLE NUMBER | ARRAY |
| `M` | `u` |
+--------------+---------------------------+
| 4 | 0 1 2 -1 |
| 5 | 0 1 2 -2 -1 |
| 6 | 0 1 2 3 -2 -1 |
| 7 | 0 1 2 3 -3 -2 -1 |
+--------------+---------------------------+
对于M > 7:
- 数组的左半部分是:
[0, 1 , 2, 3, [...], floor(M/2)]
- 数组的右半部分是:
lang-none [(-1)*(x-0), (-1)*(x-1), (-1)*(x-2), [...], -3, -2, -1] 其中 x 等于 floor((M-1)/2)
将所有Matlab代码翻译成Python
以下 Python 脚本与 Matlab 脚本具有相同的输出:
import numpy as np
import itertools as itts
def generate_data(array_size : int) -> type(np.array(range(0, 1))):
"""
+--------------+---------------------------+
| INPUT | OUTPUT |
+--------------+---------------------------+
| 4 | 0 1 2 -1 |
| 5 | 0 1 2 -2 -1 |
| 6 | 0 1 2 3 -2 -1 |
| 7 | 0 1 2 3 -3 -2 -1 |
+--------------+---------------------------+
* the left side of the array:
starts at:
zero
ends at:
floor(M/2)
counts by:
+1
looks like:
[0, 1 , 2, 3, [...], floor(M/2)]
* the right side of the array...
starts at
(-1) * floor((M-1)/2)
ends at:
-1
counts by:
-1
looks like:
[
(-1) * floor((M-1)/2),
(-1) * (floor((M-1)/2) - 1),
(-1) * (floor((M-1)/2) - 2),
[...],
-3,
-2,
-1
]
"""
# clean_input = int(dirty_input)
n = int(array_size)
# make the first element of the left side of the array be zero.
# left_side_first = 0
lsf = 0
# left_side_last = clean_input // 2
lsl = n // 2
# left_side_iterator = range(left_side_first, 1 + left_side_last)
lsit = range(lsf, 1 + lsl)
# `list` stands for "left side iterator"
right_side_first = (-1) * ((n - 1) // 2)
right_side_last = -1
right_side_iterator = range(right_side_first, 1 + right_side_last)
# merged_iterator = chain(left_side_iterator, right_side_iterator)
merged_iterator = itts.chain(lsit, right_side_iterator)
output = np.array(list(merged_iterator))
# We convert the iterator to a `list` because the following
# direct use of the iterator does not work:
#
# output = np.array(merged_iterator)
return output
我们可以像这样调用 Python 函数:
arr = generate_data(14)
print(arr)
输入14 的输出如下所示:
[ 0 1 2 3 4 5 6 7 -6 -5 -4 -3 -2 -1]