Efficient way to fill vector values from another vector

2 views (last 30 days)
Hi,
Sorry for a similar topic to my last question, but I am finding it difficult to think outside loops.
Given 2 vectors, one signal and one values, I would like to produce a 3rd output vector which replaces the zeros of the signal input with the value of the previous corresponding 1.
For example in the sample inputs below, the first two 0's are given with 45, which is the value corresponding to the first 1 after these zeros. Each 1 will have it's corresponding value
inputSignal = [1;0;0;1;1;1;0;1]; inputTargets = [34;46;54;45;34;22;12;34]; retTargets = [34;45;45;45;34;22;34;34];
I have made various attempts and the following is the fastest I can get it over large input vectors. I got the strfind function from a previous post, but I cannot find a way to do the fill without the for loop. It works fine, but I am wondering if there is a neater vectorized way. I have used find() in a loop but this is very slow.
inputSignal = [1;0;0;1;1;1;0;1];
inputTargets = [34;46;54;45;34;22;12;34];
rollingTargets = flipud(inputTargets);
rollingTargets1 = rollingTargets;
retTargets = rollingTargets;
%Find the 1's
a = strfind([flipud(inputSignal)' 0], [1 0]);
b = a(2:end);
%Fill the zeros
for i=1:size(b, 2)
rollingTargets(a(i):b(i)) = rollingTargets1(a(i));
end
rollingTargets(a(i+1):end) = rollingTargets1(a(end));
%fill the 1's
retTargets = flipud(rollingTargets);
c = find(inputSignal);
retTargets(c) = inputTargets(c);

Accepted Answer

Teja Muppirala
Teja Muppirala on 19 Apr 2011
Sometimes the answer is simpler than you think:
retTargets = inputSignal.*inputTargets;
for n = numel(retTargets)-1:-1:1
if retTargets(n) == 0
retTargets(n) = retTargets(n+1);
end
end
  3 Comments
RoJo
RoJo on 20 Apr 2011
Thanks. I was hoping to do it without loops or if's in case my vector sizes grow alot (just because of what I have read about loop performance on the forums). But it is tidy and quicker than my attempt for my vector size at the moment.
Oleg Komarov
Oleg Komarov on 20 Apr 2011
If you preallocate properly (as done here), loops can be faster in many cases.

Sign in to comment.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 19 Apr 2011
old variant
p=flipud(inputSignal);
p1 = flipud(inputTargets);
ne = diff(find(diff([~p(1); p(:); ~p(end)]))); %idea by Walter Roberson
np = length(p);
I = (1:np).';
Ifirst = I([true; diff(p)~=0]); % idea by Matt Fig
z = p(Ifirst)==0;
if z(1) , z(1)=0; end
Nums = p1(Ifirst(z)-1);
Cns = mat2cell(I,ne,1);
p1(cat(1,Cns{z}) ) = cell2mat(cellfun(@(x,y)x*ones(y,1),num2cell(Nums),num2cell(ne(z)), 'UniformOutput', false));
outTargets = reshape(flipud(p1),size(inputTargets));

Oleg Komarov
Oleg Komarov on 19 Apr 2011
Two alternative methods which use the rude
tic
[len,val] = rude(inputSignal);
newval(inputSignal == 1) = find(inputSignal);
cumlen = cumsum(len)+1;
idx = ~val;
newval(inputSignal == 0) = rude(len(idx),cumlen(idx));
retTargets = inputTargets(newval);
toc
tic
retTargets(logical(inputSignal)) = inputTargets(logical(inputSignal));
[len, val] = rude(retTargets);
idx = val == 0;
val(idx) = val(find(idx) + 1);
retTargets = rude(len,val);
toc

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!