MATLAB Answers

Loop problem...please help

1 ビュー (過去 30 日間)
Tabshir Bin Bashar
Tabshir Bin Bashar 2019 年 12 月 8 日
Commented: Walter Roberson 2019 年 12 月 8 日
Write a MATLAB function that takes a one-dimensional array of numbers (either a
row or column vector), and removes all of the neighboring duplicated numbers. For
example, the array [1 2 2 2 3 0 1 0 0 4] becomes [1 2 3 0 1 0 4]. The function should
return the answer as a one-dimensional array of numbers in the same format as the
input. Your program should use a loop command.

  1 件のコメント

Walter Roberson
Walter Roberson 2019 年 12 月 8 日

サインイン to comment.

採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 8 日
Take a copy of the input. Go through it starting from the end. If the current entry is the same as the entry before it in the array, delete the current entry.

  2 件のコメント

Tabshir Bin Bashar
Tabshir Bin Bashar 2019 年 12 月 8 日
x=[1 2 2 2 3 0 1 0 0 4];
for i=1:length(x)
if x(i)==x(i+1)
x(i)=[];
end
end
tried this but not working
Walter Roberson
Walter Roberson 2019 年 12 月 8 日
Starting from the end I said, not from the beginning . And I said to check the previous entry, not the next entr.
And watch out for your boundary condition. If you are at i = 1 then you should not be examining x(i-1)

サインイン to comment.

More Answers (0)

サインイン してこの質問に回答します。

タグ


Translated by