Merging or Combining Rows

6 vues (au cours des 30 derniers jours)
Jake
Jake le 5 Mai 2013
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!

Réponse acceptée

Shashank Prasanna
Shashank Prasanna le 5 Mai 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)
  1 commentaire
Jake
Jake le 5 Mai 2013
Perfect, thank you very much.

Connectez-vous pour commenter.

Plus de réponses (1)

Roger Stafford
Roger Stafford le 5 Mai 2013
Modifié(e) : Roger Stafford le 5 Mai 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);

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by