How to supplement the matrix?
1 次查看(过去 30 天)
显示 更早的评论
Hello! I cleaned my signal from emissions, it is in the form of a matrix, but there I used i-1, in consequence of which, instead of 1100, I now have 1099 values, how can I add the last value?\
for i = 1:length(d)-1 % Error 1100 1099
if g(i)-50>= g(i+1);
f(i)=g(i);
end
end
6 个评论
Jan
2019-7-8
@Lev: |zeros()< does not convert anything to zeros. Please read the documentation:
doc zeros
to find out, that this is a pre-allocation of the output. Letting an array grow iteratively is extremely inefficient. Matlab has to allocation a new array in each iteration. So:
r = [];
for k = 1:1e6
r(k) = rand;
end
needs 8*1e6 bytes finally, but intermediately 8*sum(1:1e6) bytes are allocated: 4 TeraByte!
I do not understand the meaning of your comment. If you detect outliers by subtracting pairs elements, of course there is one element less in the output.
回答(2 个)
dpb
2019-7-5
Adding to Stephen's answer Matlab syntax, the crystal ball says
for i = 1:length(d)-1 % Error 1100 1099
if g(i)-50>= g(i+1);
f(i)=g(i);
end
end
becomes
f=zeros(size(g));
ix=diff(g)<-50;
f(ix)=g(ix);
0 个评论
Lev Mihailov
2019-7-5
5 个评论
dpb
2019-7-5
c can only have length(d)-1 elements in it...as has been said multiple times before, preallocate it to the size wanted or compensate that there will always be one less array element since you're dealing with diff() functionality that produces one less element by its very nature.
dpb
2019-7-5
编辑:dpb
2019-7-5
In
for chek = 1:length(d)-1
if g(chek )-50>= g(chek +1);
c(chek )=10;
else g(chek )+50 <= g(chek +1);
c(chek)=g(chek );
end
end
the else clause should be elseif it appears.
What is the value for c if abs(diff(g))<50?
Again, use Matlab array syntax; the above loop construct (presuming the "elseif" and zero for then missing "else" clause)
c=zeros(size(g)); % preallocate to same size as g array
ix=diff(g)<-50;
c(ix)=10;
ix=diff(g)>=50;
c(ix)=g(ix);
You need to define what happens in the other cases not satisfied...if they're to remain unchanged, then use
c=g;
for initialization instead of zeros
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!