将数组中的元素复制到Matlab中的另一个数组中 [英] Replicate elements from an array into a different array in Matlab

查看:793
本文介绍了将数组中的元素复制到Matlab中的另一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,我有一个数组延迟 (大小为1x11)和一个单元格数组 Latencies_ms (尺寸1x298)。 延迟 Latencies_ms 的一小部分,即等待时间存在于 Latencies_ms 中。我希望能够在 Latencies_ms 内的延迟中找到每个值,然后添加 1000 到该值,并在 Latencies_ms 中重复此操作8次。

In Matlab I have an array "latencies" (size 1x11) and a cell array "Latencies_ms" (size 1x298). latencies is a smaller part of Latencies_ms, i.e. the values in latencies exist within Latencies_ms. I want to be able to find each value in latencies inside Latencies_ms and then add 1000 to that value and repeat this 8 times inside Latencies_ms.

例如,

latencies = [62626  176578  284690  397708  503278  614724  722466] 

和(一小部分Latencies_ms)

and (a small sample of Latencies_ms)

Latencies_ms = {3458, 62626, 123456, 7891011, 121341, 222112, 176578}

我希望输出为

out = {3458, 62626, 63626,  64626,  65626,  66626,  67626,  68626,  69626,  70626, 123456, 7891011, 121341, 222112, 176578, 177578, 178578, 179578, 180578, 181578, 182578, 183578, 184578,}

作为起点,我决定看看是否可以仅复制每个元素而无需添加1000,并且我将以下代码与 repmat

As a starting point I decided to see if I could just replicate each element without adding 1000 and I have used the following code with repmat:

out = arrayfun( @(x,b)[x; repmat({latencies},8,1)],... 
Latencies_ms, ismember(cell2mat(Latencies_ms),latencies), 'uni', 0 );
out = vertcat(out4{:});

在这里我将延迟元素与Latencies_ms匹配,然后使用 repmat 像这样使用它会在正确的位置插入整个 latency 数组,而不是重复元素。

where I match the elements of latencies with Latencies_ms and then use repmat however using it like this inserts the entire latencies array at the correct locations rather than repeating the elements.

然后,如果我尝试使用这样的for循环:

Then if I try using a for-loop like this:

for i=1:length(latencies)
   out = arrayfun( @(x,b)[x; repmat({latencies(i)},8,1)],... 
   Latencies_ms, ismember(cell2mat(Latencies_ms),latencies), 'uni', 0 );
   out = vertcat(out4{:});
end

它只重复延迟的最后一个元素,因此可以正确地进行复制,但不能正确的元素。

it repeats just the last element of latencies so its correctly doing the replication but not of the correct elements.

我对 arrayfun 不太熟练,我的主要目的是避免使用for循环,因此我敢肯定这不是正确的方法,但是我觉得我快到了……有人知道我缺少什么吗???

I'm not too proficient with arrayfun and I think its whole point is to avoid using for-loops so I'm sure this isn't the correct way anyway but I feel like I'm almost there... Does anyone know what I am missing???

我不必使用arrayfun,我尝试使用for-loops来做到这一点,但它有点混乱,但是使用arrayfun并没有限制我只是想得到正确的输出!

I don't have to use arrayfun, I tried to do this using for-loops but it got a bit messy but there isn't a restriction on using just arrayfun I just want to get to the correct output!

推荐答案

这是一种无循环方式:

ind = ismember([Latencies_ms{:}] , latencies);      %Indices of the values to be repeated
repvals = bsxfun(@plus, repmat([Latencies_ms{ind}], 9, 1).', 0:1000:8000); %Rep+increment
out = Latencies_ms;
out(ind) = mat2cell(repvals, ones(1, sum(ind)), 9); %Replacing with repeated+inc elements
out = [out{:}]; %Converting to comma-separated list and then concatenating horizontally

因子 9 <第二行和第四行中的/ code>在那里,因为匹配的元素将保留一次,并以增量递增重复8次(总计= 9次)。第二行可以用≥R2016b的隐式扩展写成:

The factor 9 in 2nd and 4th line is there since the matched element is to be kept once and repeated 8 times with increments (total = 9 times). The second line can be written with implicit expansion in ≥ R2016b as:

repvals  = repmat([Latencies_ms{ind}], 9, 1).' + (0:1000:8000);

arrayfun 是用于一个循环。这仍然是一个循环。但是循环(从R2015b开始)在较新版本中已得到显着改善,有时它们的性能甚至超过了矢量化代码。因此,除非是瓶颈,否则无需避免复杂的向量化所能理解的简单循环。

arrayfun is a one-line wrapper for a loop. It is still a loop. But loops have been significantly improved in newer versions (starting from R2015b) and sometimes their performance even surpass the vectorised code. So no need to avoid an easy loop that you can understand for complicated vectorisation unless it's a bottle-neck.

这篇关于将数组中的元素复制到Matlab中的另一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆