how to separate string matrix by zeros

I imported some tables into matlab as string matrices. Each row contains 16 values. I would like to separte them by a group of zeros but don't know how to do it. For example, as highlighted on the imageI'd like to extract those three sections from the matrix, but don't know how to code the loop
k=1;
for i=1:size(m,1)
x = m(i,:);
str = sprintf('%s,', x{:});
num = sscanf(str, '%g,', [16, inf]);
val(:,k)=num;
if sum(val(:,k))~=0
% read more rows in until sum(num)==0
k=k+1;
else
end
end

1 件のコメント

Rik
Rik 2021 年 6 月 2 日
What is your intended output? A cell vector with one section in each cell element?

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2021 年 6 月 2 日
編集済み: Stephen23 2021 年 6 月 2 日

0 投票

This would have been easier if you had imported the data as numeric, rather than as string.
Rather than relying on low-level loops and inefficient array expansion the MATLAB approach would be to use indexing and handle the entire matrix or "non-zero blocks" at once. For example:
S = load('matlab.mat')
S = struct with fields:
m: [7195×1 string]
M = sscanf(sprintf('%s,',S.m),'%f,',[16,Inf]).' % convert to numeric
M = 7195×16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 7 5 1 7 5 0 0 8 4 3 5 2 6 1 1 0 7 5 1 7 5 0 0 8 4 3 5 2 6 1 7 2 6 0 7 7 1 1 8 0 3 0 5 4 7 7 7 2 6 0 7 7 2 1 8 0 3 0 5 4 7 7 7 7 10 13 13 13 14 14 16 9 10 10 12 13 12 13 7 7 10 13 13 13 14 14 16 9 10 10 13 12 13 13 15 9 9 9 10 10 10 9 15 14 15 15 15 14 14 13
Your question is unclear what you expect for the output. If you want one numeric matrix with all "non-zero" rows in it, then just use basic MATLAB indexing:
X = any(M~=0,2);
Z = M(X,:)
Z = 6199×16
1 0 7 5 1 7 5 0 0 8 4 3 5 2 6 1 1 0 7 5 1 7 5 0 0 8 4 3 5 2 6 1 7 2 6 0 7 7 1 1 8 0 3 0 5 4 7 7 7 2 6 0 7 7 2 1 8 0 3 0 5 4 7 7 7 7 10 13 13 13 14 14 16 9 10 10 12 13 12 13 7 7 10 13 13 13 14 14 16 9 10 10 13 12 13 13 15 9 9 9 10 10 10 9 15 14 15 15 15 14 14 13 15 9 9 9 10 10 10 9 15 14 15 15 15 14 14 13 13 12 13 14 14 14 13 13 13 13 12 11 13 13 12 13 13 12 13 14 14 14 13 13 13 13 12 11 13 13 12 13
If you want to keep those "non-zero groups" separate then you should split them into a cell array:
D = diff([false;X;false]);
Y = find(D<0)-find(D>0);
C = mat2cell(M(X,:),Y,16);
Checking the first three matrices:
C{1:3}
ans = 12×16
1 0 7 5 1 7 5 0 0 8 4 3 5 2 6 1 1 0 7 5 1 7 5 0 0 8 4 3 5 2 6 1 7 2 6 0 7 7 1 1 8 0 3 0 5 4 7 7 7 2 6 0 7 7 2 1 8 0 3 0 5 4 7 7 7 7 10 13 13 13 14 14 16 9 10 10 12 13 12 13 7 7 10 13 13 13 14 14 16 9 10 10 13 12 13 13 15 9 9 9 10 10 10 9 15 14 15 15 15 14 14 13 15 9 9 9 10 10 10 9 15 14 15 15 15 14 14 13 13 12 13 14 14 14 13 13 13 13 12 11 13 13 12 13 13 12 13 14 14 14 13 13 13 13 12 11 13 13 12 13
ans = 13×16
0 0 0 0 0 0 0 0 0 0 0 1 7 13 14 13 0 0 0 0 0 0 0 0 0 0 0 2 10 13 14 13 14 11 6 6 7 7 6 7 4 12 12 13 14 13 15 14 14 11 6 6 7 7 6 7 4 12 12 13 14 13 15 14 13 13 14 14 14 14 13 12 13 14 13 14 14 13 12 12 13 13 14 14 14 14 13 12 13 14 13 14 14 13 12 12 12 12 12 9 11 7 12 14 15 15 13 9 9 9 9 9 12 12 12 9 11 7 12 14 15 15 13 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 14 13 13 9 9 9 9 9 9 9 9 9 9 9 9 9 14 13 13
ans = 58×16
1 7 12 13 11 12 6 2 8 6 4 2 6 2 6 0 1 7 12 13 11 12 6 2 8 6 4 2 6 2 6 0 0 0 0 0 7 7 0 2 0 3 4 0 7 5 1 0 1 0 0 0 7 7 1 0 5 0 4 0 5 5 1 0 7 5 2 7 7 0 1 4 4 4 1 7 7 1 0 0 7 5 2 7 7 0 1 4 4 4 1 7 7 1 0 0 6 5 1 6 7 2 0 6 4 1 7 7 2 1 7 4 6 5 1 6 7 2 0 6 4 1 7 7 2 1 7 4 5 0 3 7 1 0 7 4 1 2 7 7 1 1 8 2 5 0 3 7 1 3 7 6 0 7 5 7 1 0 6 3

1 件のコメント

Josh
Josh 2021 年 6 月 2 日
thanks, I want separted matrices.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 6 月 2 日

0 投票

id = zeros([],1) ; % indices of required strings
count = 0;
for i = 1:size(m,1)
t = str2num(m(i,:)) ;
if any(t)
count = count+1 ;
id(count) = i ;
end
end
iwant = m(id,:)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2021 年 6 月 2 日

コメント済み:

2021 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by