text into matrix cell

5 views (last 30 days)
Heryswe on 1 Mar 2012
Hello, here is my problem: I computed a matrix, 48x48, filled with values. I'm using then other very specific, user-unfriendly program that wants me to enter rows in the format: no.of row:value To show it on example:
original matrix:
250 120 25 ...
110 320 100 ...
desired matrix:
1:250 1:120 1:25 ...
2:110 2:320 2:100 ...
Anyone has an idea how to do this?
Thank you very much!

Answers (3)

Jarrod Rivituso
Jarrod Rivituso on 1 Mar 2012
My solution would involve meshgrid and some fancy arrayfun action (though you could easily replace arrayfun with a for loop)
%Some matrix
data = rand(5)
%Get a matrix of row numbers
rows = 1:size(data,1);
cols = 1:size(data,2);
[rowNums,colNums] = meshgrid(rows,cols);
%Combine it with original data
combineFunction = @(rownum,valnum) [num2str(rownum) ':' num2str(valnum)];
res = arrayfun(combineFunction, rowNums, data, 'UniformOutput', false)
Note you could then quite easily use something like xlswrite to export the res matrix.
Jarrod Rivituso
Jarrod Rivituso on 2 Mar 2012
this is true, good point. i originally had meshgrid with one output argument, but that just looked weird to me :)

Sign in to comment.

G A on 1 Mar 2012
A is your matrix kxk
C = cell(k,k);
for n = 1:k
for m = 1:k
C{m,n} = [num2str(m),':',num2str(A(m,n))];

Heryswe on 1 Mar 2012
thanks guys, both ways work for me!

Community Treasure Hunt

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

Start Hunting!