replace the repeated number in a given sequence with value of zero

1 回表示 (過去 30 日間)
Kundan Prasad
Kundan Prasad 2022 年 2 月 15 日
回答済み: DGM 2022 年 2 月 15 日
Dear Sir/Madam,
I want to replace the value of Z2,Z4,Z6,Z7,Z9,Z10,Z12,Z13... and so on with value of zero.
I have attached the same.
Thank you
r=22.5;
R=linspace(0,10,10);
x=repelem(R,3);
z=-(r-sqrt(r^2-R.^2));
Z=repelem(z,3);
Z(2:3:end)= Z(2:3:end)-1.04;
plot(R,z,'.');hold on
plot(x,Z,'-');
output

採用された回答

DGM
DGM 2022 年 2 月 15 日
This can probably be simplified, but eh.
r=22.5;
R=linspace(0,10,10);
x=repelem(R,3);
z=-(r-sqrt(r^2-R.^2));
Z=repelem(z,3);
Z(2:3:end)= Z(2:3:end)-1.04;
plot(R,z,'.');hold on
plot(x,Z,'-');
Z0 = Z;
tol = 1E-15; % or pick some tolerance
uz = unique(Z);
for k = 1:numel(uz)
idx = find(abs(Z-uz(k))<tol);
if numel(idx)>1
Z(idx(2:end)) = 0; % get rid of the rightmost instances
%Z(idx(1:end-1)) = 0; % get rid of the leftmost instances
end
end
[Z0' Z']
ans = 30×2
0 0 -1.0400 -1.0400 0 0 -0.0275 -0.0275 -1.0675 -1.0675 -0.0275 0 -0.1100 -0.1100 -1.1500 -1.1500 -0.1100 0 -0.2483 -0.2483
Z now only contains unique values.
If the duplicate values are created with a known period, it may suffice to directly remove them without the need for comparison.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by