lax*_*xer 2 arrays indexing matlab matrix
在 Matlab 中,除了示例之外,我不知道解释这一点的最佳方法。假设我有一个名为的数组tStart
和一个tDuration
长度:
tStart = [3,8,15,20,25];
tDuration = 2;
Run Code Online (Sandbox Code Playgroud)
有没有办法获得一个新数组,使其成为:
[3,4,5,8,9,10,15,16,17,20,21,22,25,26,27]
Run Code Online (Sandbox Code Playgroud)
所以我想要的是使用初始tStart
数组,然后用起始值组成一个新数组,然后是tDuration
.
如果我这样做了,[tStart(1:end)+tDuration]
我会得到一个结束值数组,但是我怎样才能得到开始、结束以及它们之间的所有值呢?
如果我[tStart(1:end):tStart(1:end)+tDuration]
收到错误。
非常感谢在没有循环的情况下执行此操作的任何帮助。
我会使用 MATLAB 的隐式扩展、重塑和二维数组的排序。
首先,创建一个包含所需值的二维数组tStart
:
tStart = [3,8,15,20,25];
tDuration = 2;
tDurAdd = [0:tDuration].'; % numbers to add to tStart
tArray = tStart + tDurAdd;
Run Code Online (Sandbox Code Playgroud)
这给了我们
tArray =
3 8 15 20 25
4 9 16 21 26
5 10 17 22 27
Run Code Online (Sandbox Code Playgroud)
这些是正确的值,现在我们只需要将它们重塑为行向量:
tResult = reshape(tArray, 1, []);
Run Code Online (Sandbox Code Playgroud)
最后的数组是:
tResult =
3 4 5 8 9 10 15 16 17 20 21 22 25 26 27
Run Code Online (Sandbox Code Playgroud)
当然,这一切都可以在一行上完成:
tResult = reshape(tStart + [0:tDuration].', 1, []);
Run Code Online (Sandbox Code Playgroud)