text into matrix cell

조회 수: 2 (최근 30일)
Heryswe
Heryswe 2012년 3월 1일
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!
Martin

답변 (3개)

Jarrod Rivituso
Jarrod Rivituso 2012년 3월 1일
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.
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 3월 1일
You do not use colNums, so you do not need to have it as an output argument from meshgrid()
(using meshgrid / ndgrid with a single output was my thought when I looked through the problem earlier today.)
Jarrod Rivituso
Jarrod Rivituso 2012년 3월 2일
this is true, good point. i originally had meshgrid with one output argument, but that just looked weird to me :)

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


G A
G A 2012년 3월 1일
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))];
end
end

Heryswe
Heryswe 2012년 3월 1일
thanks guys, both ways work for me!

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by