So I have a large matrix(4091252x2) that looks something like this:
439105 1053224
439105 1696241
439105 580064
439105 1464748
1836139 1593258
1464748 439105
1464748 1053224
1464748 1696241
1464748 580064
580064 439105
Basically, the matrix represents calls made from one person to another, represented by a personID (439105 calls 1053224). What I want to do is scale down this matrix so that the smallest personID = 1, and 2 for the next lowest personID, 3 for next lowest personID after that, etc. For example if the matrix looked like this:
110 503
402 110
300 900
300 402
402 110
I would want it mapped to:
1 4
3 1
2 5
2 3
3 1
The problem is that I'm a beginner to Matlab, and I have no idea how to do this. I looked into reshape and sub2ind but I don't really think thats what I'm looking for. How would I accomplish this in Matlab?
Any help would be greatly appreciated, thanks!
You can use the third output of unique
for this purpose, it just needs reshaping.
A=[110 503
402 110
300 900
300 402]
[~,~,D]=unique(A);
reshape(D,size(A))
For a problem like this, the function unique
is your friend. Type help unique
to get more information.
For this problem, if you have the input:
input = [[110 503];
[402 110];
[300 900];
[300 402];
[402 110]];
You can get the desired mapping as follows:
output = input;
[C,IA,IC] = unique(input(:));
output(:) = IC;
Which will yield a desired output:
output =
1 4
3 1
2 5
2 3
3 1
IC contains indices (that's the "I" in "IC") so they'll be values from 1 to the number of unique values in your input array. If you'd like to use some other tokens, you can use IC to index into another array of unique identifiers.
FYI, on my Macbook Pro, doing this operation on a 4091252x2 array takes about 1.2 seconds.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.