在matlab中选择数组的特定元素

时间:2014-03-31 18:55:48

标签: arrays matlab matrix-indexing

如何选择N个元素然后忽略接下来的N个元素,然后选择接下来的N个,依此类推?

阵列的例子a = 1:100;

b = 1,2,3,7,8,9,13,14,15,......

1 个答案:

答案 0 :(得分:1)

方法#1

a1 = reshape([a zeros(1,2*N - rem(numel(a),2*N))],N,[]);
out = reshape(a1(:,1:2:end),1,[]);
if rem(numel(a),2*N)<N
    out = out(1:N*floor(numel(a)/(2*N)) + rem(numel(a),2*N)); %//output
end

方法#2

a1 = vec2mat(a,N)'; %//'
out = reshape(a1(:,1:2:end,:),1,[]);
if rem(numel(a),2*N)<N
    out = out(1:N*floor(numel(a)/(2*N)) + rem(numel(a),2*N)); %//output
end

方法#3

mat1 = [true false];
mat2 = reshape(mat1(ones(N,1),:),[],1);
mat3 = reshape(mat2(:,ones(1,ceil(numel(a)/(2*N)))),[],1);
out = a(mat3(1:numel(a)));  %//output