Merging or Combining Rows
7 views (last 30 days)
Show older comments
I have a matrix that looks like this:
86 34
23 59
93 45
(Random numbers) But I want to make it like this:
8634
2359
9345
Basically, making each row of 2 numbers each into 1 number. I'm sure there is a basic solution to this, however I am new to Matlab.
The point of this little exercise is to find the unique number sequence. I tried summing along the rows and using "unique" but the problem was that some rows summed to the same value, even with different numbers. i.e.:
72 34
34 72
These would sum to the same number and one would be removed by "unique", even though their order is different.
I hope this was clear. Thank you!
0 Comments
Accepted Answer
Shashank Prasanna
on 5 May 2013
I am sure there are quick way to solve the unique problem, but you can merge the two numbers by converting them to strings and back:
% If A is your matrix
>> A = [86 34
23 59
93 45
72 34
34 72];
>> B = num2str(A);
>> B(:,3:4) = [];
>> B = str2num(B);
>> unique(B)
More Answers (1)
Roger Stafford
on 5 May 2013
Edited: Roger Stafford
on 5 May 2013
For your stated purpose you should use 'unique' with the 'rows' option.
To answer your question, however, do the following. This assumes that all the numbers in A are integers somewhere in the interval from 0 to 99.
B = 100*A(:,1)+A(:,2);
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!