0

I want to make a Matlab function that takes two matrices A and B (of the same size) and combines them in a certain way to give an output that can be used in Latex - table.

I want the first row of the output matrix to consist of the first row of matrix A, with ampersands (&) in between them, and that ends with an double backslash.

The second row should be the first row of B with parentheses around them, and ampersands in between. And so on for the rest of A and B.

If I let A=rand(1,2), I could do this by using [num2str(A(1)), ' & ', num2str(A(2)),' \\'] and so on.

But I want to be able to make a function that does this for any size of the matrix A. I guess I have to make cell structures in some way. But how?

1
  • So basically you are trying to print the Matrix C to latex code? C=nan(size(A).*[2,1]);C(1:2:end)=A;C(2:2:end)=B; Commented Feb 7, 2015 at 13:06

2 Answers 2

1

This could be one approach -

%// First off, make the "mixed" matrix of A and B
AB = zeros(size(A,1)*2,size(A,2));
AB(1:2:end) = A;
AB(2:2:end) = B;

%// Convert all numbers of AB to characters with ampersands separating them
AB_amp_backslash = num2str(AB,'%1d & ');

%// Remove the ending ampersands
AB_amp_backslash(:,end-1:end) = [];

%// Append the string ` \\` and make a cell array for the final output
ABcat_char = strcat(AB_amp_backslash,' \\');
ABcat_cell = cellstr(ABcat_char)

Sample run -

A =
   183   163   116    50
   161    77   107    91
   150   124    56    46
B =
   161   108   198     4
   198    18    14   137
     6   161   188   157
ABcat_cell = 
    '183 & 163 & 116 &  50 \\'
    '161 & 108 & 198 &   4 \\'
    '161 &  77 & 107 &  91 \\'
    '198 &  18 &  14 & 137 \\'
    '150 & 124 &  56 &  46 \\'
    '  6 & 161 & 188 & 157 \\'
Sign up to request clarification or add additional context in comments.

6 Comments

What does the %1d do?
@Studenttea Well with num2str('%1d'), we are telling MATLAB to convert all numbers to characters and no space separating two numbers. But there could still be spacing left if the number of digits between numbers of the same column differ. So that %1d is the signal to MATLAB to have no extra spacing added. With '%1d & ', we have added few more stuffs and telling MATLAB to also concatenate a space and an ampersand and another space beforing concatenating the next element in the same row. Hope this made sense!
If the matrix AB is printed, it is on decimal format, but the output is on the form nne+nn ("scientific?"). Where do we tell matlab to do that? I would prefer decimal (0.09988) .
@Studenttea Try with num2str(AB,'%g & ') if that gives you the intended output.
Looks good. How would you add the parentheses around the numbers? so that every second line has parentheses around the numbers as described in the question? Like this: colby.edu/economics/faculty/jmlong/ec333/meier-rauch.pdf
|
1

You can use sprintf, it will repeat the format spec as many times as required until all input variables are processed:

%combine both to one matrix
C=nan(size(A).*[2,1]);
C(1:2:end)=A;
C(2:2:end)=B;
%print
sprintf('%f & %f \\\\\n',C.')

The transpose (.') is required to fix the ordering.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.