在matlab中比较和移动不同长度的字符串/数组 [英] comparison and shifting strings/arrays with different length in matlab
问题描述
我有一个不同长度或大小的字符串或数组的列表。我想使用最短的字符串,并与其他字符串进行比较,通过移动最短的字符串窗口一个接一个做比较。
I have a list of strings or arrays with different length or size. I want to use the shortest string and compare with other strings by shifting the shortest string window one by one to do comparison.
假设我想添加,我已将 [2 1 3]
作为我的最短名单, [4 5 7 8 9]
Let's say I want to do addition, I have [2 1 3]
as my shortest list and want to perform addition on [4 5 7 8 9]
1st addition: [2 1 3] + [4 5 7]
2nd addition: [2 1 3] + [5 7 8]
3rd addition: [2 1 3] + [7 8 9]
感谢您使用matlab来实现这个功能。
How can i do this using matlab?
推荐答案
一个更简单的方法,如果你不关心速度是使用 arrayfun
和 cell2mat
。注意,这种方法不检查哪个向量是哪个。 a 必须短于
b
。
A simpler approach, if you don't care much about speed is using arrayfun
and cell2mat
. Note that this approach doesn't check which vector is which. a
must be shorter than b
.
a =
1 2 3
b =
1 3 5 2 4 6
c = cell2mat(arrayfun(@(n) a+b(n:n+numel(a)-1), 1:numel(b)-numel(a)+1,'UniformOutput',0).')
c =
2 5 8
4 7 5
6 4 7
3 6 9
这篇关于在matlab中比较和移动不同长度的字符串/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!