开发者

How to eliminate repeated data using MATLAB

I listed my data to something like this. I want to eliminate the repeated data in each row. How can I do that using MATLAB?

13  1开发者_如何学JAVA3  13  13  38  38  38

13  13  42  0   0   0   0

Expected result:

13 38

13 42


Have a look into the function unique. Check out the documentation here.

One way of operating on each of the rows of a matrix would be to call unique inside a loop for each row. Obviously, you could end up with different numbers of unique elements for each row, so you may have to store the result in a cell array.

Hope this helps.


To select unique elements from a vector, you can do:

a = unique(b, 'first');

You can find more about this function from Mathworks site docs.

Update

Building on what Amro said, you could do something like this if the top and bottom aren't guaranteed to be the same length (I'm guess they aren't, since that seems like an unlikely event):

result = {}
for i = 1:size(a, 1)
  result{i} = unique(a(i, :), 'first');
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜