在Matlab中向量化一维向量上的范围选择 [英] vectorize selection of ranges on a 1D vector in Matlab
本文介绍了在Matlab中向量化一维向量上的范围选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能很简单,但是我无法弄清楚...我想创建一个范围矩阵,可以使用以下循环执行此操作:
This is probably very simple, but I can't figure it out... I want to create a matrix of ranges and I can do this using the following loop:
a=[0 10 22 35 42]; % sample initial ranges
for i=1:length(a)
b(i,:)= a(i):a(i)+5;
end
b =
0 1 2 3 4 5
10 11 12 13 14 15
22 23 24 25 26 27
35 36 37 38 39 40
42 43 44 45 46 47
如何将其向量化?
推荐答案
a = 0:10:40;
b = bsxfun(@plus,a', 0:5)
b =
0 1 2 3 4 5
10 11 12 13 14 15
20 21 22 23 24 25
30 31 32 33 34 35
40 41 42 43 44 45
这篇关于在Matlab中向量化一维向量上的范围选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文