Efficient way to fill vector values from another vector
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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);
0 Kommentare
Akzeptierte Antwort
Teja Muppirala
am 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 Kommentare
Oleg Komarov
am 20 Apr. 2011
If you preallocate properly (as done here), loops can be faster in many cases.
Weitere Antworten (2)
Andrei Bobrov
am 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));
0 Kommentare
Oleg Komarov
am 19 Apr. 2011
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!