reallocating matrix concatenated as string

조회 수: 1 (최근 30일)
Berfin Çetinkaya
Berfin Çetinkaya 2022년 3월 24일
댓글: Berfin Çetinkaya 2022년 3월 24일
birlestirbirincifaz = cell(20,69) ;
for i = 1:20
for j = 1:69
birlestirbirincifaz{i,j} = [num2str(sonucmatrisi(i,j)),'-',num2str(faz1atananmakineler(i,j))] ;
end
end
I combined two matrices as strings using this code.
But now I have to take it apart again. What should I do?
(small snippet of my matrix)
'10-10' '22-22' '42-42'
'193.92-8' '68.8-7' '9.34-1'
'193.92-8' '68.8-8' '9.34-3'

채택된 답변

Stephen23
Stephen23 2022년 3월 24일
편집: Stephen23 2022년 3월 24일
Much better to use string arrays:
M1 = [10,22,42;193.92,68.8,9.34;193.92,68.8,9.34]
M1 = 3×3
10.0000 22.0000 42.0000 193.9200 68.8000 9.3400 193.9200 68.8000 9.3400
M2 = [10,22,42;8,7,1;8,7,1]
M2 = 3×3
10 22 42 8 7 1 8 7 1
S = M1+"-"+M2
S = 3×3 string array
"10-10" "22-22" "42-42" "193.92-8" "68.8-7" "9.34-1" "193.92-8" "68.8-7" "9.34-1"
T = split(S,'-')
T = 3×3×2 string array
T(:,:,1) = "10" "22" "42" "193.92" "68.8" "9.34" "193.92" "68.8" "9.34" T(:,:,2) = "10" "22" "42" "8" "7" "1" "8" "7" "1"
str2double(T(:,:,1))
ans = 3×3
10.0000 22.0000 42.0000 193.9200 68.8000 9.3400 193.9200 68.8000 9.3400
str2double(T(:,:,2))
ans = 3×3
10 22 42 8 7 1 8 7 1
  댓글 수: 2
Steven Lord
Steven Lord 2022년 3월 24일
With a string array containing the text representations of numbers you don't need to use str2double. Just calling double is sufficient.
M1 = [10,22,42;193.92,68.8,9.34;193.92,68.8,9.34];
M2 = [10,22,42;8,7,1;8,7,1];
S = M1+"-"+M2;
T = split(S,'-');
M1After = double(T(:,:,1))
M1After = 3×3
10.0000 22.0000 42.0000 193.9200 68.8000 9.3400 193.9200 68.8000 9.3400
M2After = double(T(:,:,2))
M2After = 3×3
10 22 42 8 7 1 8 7 1
Berfin Çetinkaya
Berfin Çetinkaya 2022년 3월 24일
Thank you for help !

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

추가 답변 (1개)

Riccardo Scorretti
Riccardo Scorretti 2022년 3월 24일
If I understand well your question, you wish to do this:
sonucmatrisi = zeros(20, 69);
faz1atananmakineler = zeros(20, 69);
for i = 1:20
for j = 1:69
tmp = sscanf(birlestirbirincifaz{i,j}, '%f-%f');
sonucmatrisi(i,j) = tmp(1);
faz1atananmakineler(i,j) = tmp(2);
end
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by