【发布时间】:2015-01-18 20:25:16
【问题描述】:
我有 2 个数据向量 d1 = [1 2 1 3 4] 和 d2 = [3 1 2 1],以及 2 个引用 howMany1= [3 2 2 1 2] 和 howMany2 = [2 1 2 2]。 d1 和 d2 的元素要根据 howMany1 和 howMany2 的元素重复:所以 d1(1) 即 d1 的第一个元素应该重复 3 次,d1(2) 应该重复 2 次,依此类推。最终结果应该是d1_repeated = [1 1 1 2 2 1 1 3 4 4] 和d2_repeated = [3 3 1 2 2 1 1]
我怎么能在 MATLAB 中做到这一点?我查看了一篇类似的帖子,其中重复了一个向量,所以我尝试做同样的事情并制作了一个 for 循环。这是我的代码:
clear all
close all
clc
% data
d1 = [1 2 1 3 4];
d2 = [3 1 2 1];
howMany1 = [3 2 2 1 2]; % Determines how many times each index in IDX1 should be repeated.
howMany2 = [2 1 2 2];
d = {d1 d2}
howMany = {howMany1 howMany2}
Anew = cell(1,2)
for k = 1:2 % 2 data vectors
Anew{1,k} = arrayfun(@(x) repmat(d{k}(x), howMany{k}(x), 1), 1:numel(d{k}), 'uni', 0);
Anew = vertcat(Anew{:});
end
但我收到以下错误:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
然后我尝试将 vert cat 更改为 horzcat,但我收到了作为双打组的重复:
Anew =
Columns 1 through 7
[3x1 double] [2x1 double] [1] [2x1 double] [2x1 double] [2x1 double] [3]
Column 8
[2x1 double]
我想知道这里有什么问题?感谢您抽出宝贵时间。
【问题讨论】:
-
除了flag中的重复,另见stackoverflow.com/q/1975772/2778484。