separating numbers in cells

조회 수: 4 (최근 30일)
Berfin Çetinkaya
Berfin Çetinkaya 2022년 5월 19일
댓글: Berfin Çetinkaya 2022년 5월 19일
I have a matrices like :
1-8 7-1 8-4
4-6 8-5 7-3
I want to separate it as two matrices.
first matrix :
1 7 8
4 8 7
second matrix:
8 1 4
6 5 3
How can I do that?
Thank you

채택된 답변

Stephen23
Stephen23 2022년 5월 19일
Assuming that you have a cell array of character vectors:
C = {'1-8','7-1','8-4';'4-6','8-5','7-3'}
C = 2×3 cell array
{'1-8'} {'7-1'} {'8-4'} {'4-6'} {'8-5'} {'7-3'}
D = split(C,'-');
A = str2double(D(:,:,1))
A = 2×3
1 7 8 4 8 7
B = str2double(D(:,:,2))
B = 2×3
8 1 4 6 5 3

추가 답변 (1개)

David Hill
David Hill 2022년 5월 19일
Do you mean a cell array like this?
a={'1-8','7-1','8-4';'4-6','8-5','7-3'};
m=zeros(size(a));n=zeros(size(a));
for k=1:numel(a)
m(k)=str2double(a{k}(1));
n(k)=str2double(a{k}(3));
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by