I am trying to write a convertor, for converting a text file to another text file. I read three different matrix, and I want to combine them.
In the following, I put a small set of data.
The first matrix is this:
0 1
3 0
1 4
This is the second matrix:
-0
-1
-0
As you see, matrix one and two have the same number of rows. This is the third matrix:
0 1
0 2
2 1
3 0
3 2
2 0
1 4
1 6
I want to produce the following:
-0: 0 1
1: 0 2
2: 2 1
-1: 3 0
4: 3 2
5: 2 0
-0: 1 4
7: 1 6
I was wondering if you kindly help me.
Thanks

댓글 수: 3

Ali
Ali 2016년 5월 30일
Please write me if you have any ideas
what is this?
-0: 0 1
Ali
Ali 2016년 5월 31일
Many thanks. I want to print data in this way.

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

 채택된 답변

Walter Roberson
Walter Roberson 2016년 5월 31일

1 개 추천

firstcol = cellstr( num2str( (0:size(Third,1)-1).', '%d:') );
[tf, idx] = ismember(Third, First, 'rows');
firstcol(tf) = Second(idx(tf));
Output = [firstcol, num2cell(Third)];

댓글 수: 5

Ali
Ali 2016년 5월 31일
Thanks a lot for your help.
I got the following message by running your code.
Conversion to cell from double is not possible.
Error in ff (line 17)
firstcol(tf) = Second(idx(tf));
Walter Roberson
Walter Roberson 2016년 5월 31일
Your Second should be a cell array of strings, not numeric values. It is tricky to determine whether a 0 is +0 or -0, and -0 easily lose their identity. -0 does exist in MATLAB as something different than 0 but with the amount of programming you have to put in to take care of it, it is easier to use cell array of strings.
Ali
Ali 2016년 5월 31일
Many thanks.
I changed the second matrix to the following, but I got the same error.
Could you help me to solve it.
-2
-1
-2
firstcol = (0:size(Third,1)-1).';
[tf, idx] = ismember(Third, First, 'rows');
firstcol(tf) = Second(idx(tf));
fprintf( '%3d: %3d %3d\n', [firstcol, Third].');
Ali
Ali 2016년 5월 31일
Thank you very much Walter Roberson . You really helped me here.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

Ali
2016년 5월 30일

댓글:

Ali
2016년 5월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by