Merging or Combining Rows

조회 수: 2 (최근 30일)
Jake
Jake 2013년 5월 5일
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!

채택된 답변

Shashank Prasanna
Shashank Prasanna 2013년 5월 5일
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
Jake
Jake 2013년 5월 5일
Perfect, thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2013년 5월 5일
편집: Roger Stafford 2013년 5월 5일
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);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by