How to eliminate repeated data using MATLAB - 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 13 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;

Related

Sort pairs according to secon entry

I really need help:(
I have a 2x367 matrix, where the first entry in each row indicates the number of the feature and the second one the number of occurences. I managed to sort them row by row, but I want to sort them pairwise.
So my data looks like:
2490 1
44 512 ......
indicating that I have feature 2490 44 times and 1 512 times. I want to put feature 1 in front but together with the 512..Thanks!
Here is another elegant way (in my opinion):
y = sortrows(x', 1)';
If your expected output is:
1 2490
512 44
you ca use the sort function:
[a,b]=sort(x(1,:))
k=[a;x(2,b)]
The function sort returns as first output the sorted values (in this case the sorte3d values of the first row) and as second output the position index.
You can use the position index to "align" the data of the second row.
Hope this helps.
Qapla'

Matlab general matrix indexing for accesing several rows

Edit for clarity:
I have two matrices, p.valor 2x1000 and p.clase 1x1000. p.valor consists of random numbers spanning from -6 to 6. p.clase contains, in order, 200 1:s, 200 2:s and 600 3:s. What I wan´t to do is
Print p.valor using a diferent color/prompt for each clase determined in p.clase, as in following figure.
I first wrote this, in order to find out which locations in p.valor represented where the 1,2 respective 3 where in p.clase
%identify the locations of all 1,2 respective 3 in p.clase
f1=find(p.clase==1);
f2=find(p.clase==2);
f3=find(p.clase==3);
%define vectors in p.valor representing the locations of 1,2,3 in p.clase
x1=p.valor(f1);
x2=p.valor(f2);
x3=p.valor(f3);
There is 200 ones (1) in p.valor, thus, is x1=(1:200). The problem is that each number one(1) (and, respectively 2 and 3) represents TWO elements in p.valor, since p.valor has 2 rows. So even though p.clase and thus x1 now only have one row, I need to include the elements in the same colums as all locations in f1.
So the different alternatives I have tried have not yet been succesfull. Examples:
plot(x1(:,1), x1(:,2),'ro')
hold on
plot(x2(:,1),x2(:,2),'k.')
hold on
plot(x3(:,1),x3(:,2),'b+')
and
y1=p.valor(201:400);
y2=p.valor(601:800);
y3=p.valor(1401:2000);
scatter(x1,y1,'k+')
hold on
scatter(x2,y1,'b.')
hold on
scatter(x3,y1,'ro')
and
y1=p.valor(201:400);
y2=p.valor(601:800);
y3=p.valor(1401:2000);
plot(x1,y1,'k+')
hold on
plot(x2,y2,'b.')
hold on
plot(x3,y3,'ro')
My figures have the axisies right, but the plotted values does not match the correct figure provided (see top of the question).
Ergo, my question is: how do I include tha values on the second row in p.valor in my plotted figure?
I hope this is clearer!
Values from both rows simultaneously can be accessed using this syntax:
X=p.value(:,findX)
In this case, resulting X matrix will be a matrix having 2 rows and length(findX) columns.
M = magic(5)
M =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
M2 = M(1:2, :)
M2 =
17 24 1 8 15
23 5 7 14 16
Matlab uses column major indexing. So to get to the next row, you actually just have to add 1. Adding 2 to an index on M2 here gets you to the next column, or adding 5 to an index on M
e.g. M2(3) is 24. To get to the next row you just add one i.e. M2(4) returns 5.To get to the next column add the number of rows so M2(2 + 2) gets you 1. If you add the number of columns like you suggested you just get gibberish.
So your method is very wrong. Freude's method is 100% correct, it's much easier to use subscript indexing than linear indexing for this. But I just wanted to explain why what you were trying doesn't work in Matlab. (aside from the fact that X=p.value(findX findX+1000) gives you a syntax error, I assume you meant X=p.value([findX findX+1000]))

Matlab: Sum elements in array into another array

Suppose I have an array age=[16 17 25 18 32 89 43 55] which holds the ages of a certain list of people. I also have a second array called groups=[1 1 2 1 3 2 1 4] denotes to which group each person belongs, i.e the person whose age is 55 is the only person in group 4, there are three people in group 1 etc.
I want to calculate the combined sum of ages in each group. That is, the result I want to get in this case is an array of 4 elements, it's first entry containing the sum of ages of people belonging to group #1 (16+17+18+43), second entry containing the sum of ages of people belonging to group #2 (23+89) etc.
I know of course how to do this with a for loop, but is it possible to do this using some variation of sum or something similar, so as to tap into matlab's vector optimization?
The code in #Ismail's answer is fine, but you could also try this:
>> accumarray(groups', age')
ans =
94
114
32
55
I find it hard to get an appreciation from the documentation exactly what accumarray can do in its full generality, but this is a great example of a simple usage. It's worth learning how to use it effectively, as once you've worked it out it's very powerful - and it will be a lot faster (when used on a larger example) than arrayfun.
You can use arrayfun and unique as follows:
arrayfun(#(x) sum(age(groups==x)), unique(groups))

Linear Probing in Open Addressing

I have an array with size m = 11 and my hash function is Division method : h(k) = k mod m
I have an integer k = 10 and 10 mod 11 is -1 so where should I put this key in the array? I should put this key in the slot which its index is 10?
please help me thanks
EDITED : for getting my answer well for example I have integers like k = 10,22,31,4,15,28,17,88,59
the array would be like this?thanks
10 9 8 7 6 5 4 3 2 1 0 index
10 31 59 17 28 4 15 88 22 keys
As it's usually done, 10 mod 11 is 10, so yes, you'd normally use index 10.
Edit: To generalize: at least as it's normally defined, given two positive inputs, a modulo will always produce a positive result. As such, your questions about what to do with negative results don't really make sense with respect to the normal definition.
If you really do have the possibility of getting a negative result, my immediate reaction would be to switch to some language that will produce a reasonable result. If you can't do that, then you'd probably want to move the value into the correct range by adding m to the negative number until you get a number in the range [0..m) so it fits the normal definition of mod, then use that as your index.

How do I maintain rows when sorting a matrix in MATLAB?

I have a 2-by-3 matrix, and I want to sort it according to the first column. Here's an example:
data will change to --> new data
11 33 10 22
22 44 11 33
10 22 22 44
I have this code for sorting a matrix A but it doesn't work well:
sort(A,1,'ascend');
The SORTROWS function can handle that for you:
B = sortrows(A);
As #gnovice suggests, sortrows is the best solution here. You can also specify more than one output for the sort and sortrowscommands, which will return the sort index. You can use this to modify your other columns as well or just to keep track of the permutation. For example:
A=rand(10,2);
[B, idx]=sortrows(A);