Copying and replacing of some values
古いコメントを表示
Hello to everyone,
I would build a function that has to scan all the 'a' vector and in correspondence of the indexes given in 'b' has to create a new vector with almost the same values of 'a' exepted for elements that go from a(2) to a(2+n)changing it with the same value of a(2). The same for a(8) and the values from a(8)to a(8+n) changing these 4 values with the value a(8). Maybe looking at the 'c_final' vector (what I would like to find!!!) could help you better than what I've tried to explain.
a=[1 5 7 11 14 15 16 12 90 33 46 78 79 66];
b=[2 8];
n = 3;
c_final = [1 5 5 5 5 15 16 12 12 12 12 78 79 66];
Thanks in advance and Best regards.
回答 (4 件)
Babak
2012 年 10 月 9 日
a=[1 5 7 11 14 15 16 12 90 33 46 78 79 66];
b=[2 8];
n=3;
c = a;
for j=1:length(b)
if length(a)>b(j)
index = b(j);
c(index:index+n)=a(index);
end
end
c = c(1:length(a));
c
Thomas
2012 年 10 月 9 日
Another way
c=a;
for ii=1:length(b)
c(find(c==c(b(ii))):find(c==c(b(ii)))+n)=c(b(ii));
end
c
Jonathan Sullivan
2012 年 10 月 9 日
It's a little bit of a round about way, but you could leverage MATLAB's strrep function.
c = char(a);
for ii = 1:length(b)
c = strrep(c,aorig(b(ii)),char(repmat(aorig(b(ii)),1,n)));
end
c = double(c);
Andrei Bobrov
2012 年 10 月 9 日
c_final = a;
c_final(bsxfun(@plus,b,(0:n)')) = repmat(a(b),n+1,1);
カテゴリ
ヘルプ センター および File Exchange で Function Creation についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!