how to separate string matrix by zeros
Mostra commenti meno recenti
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 image
I'd like to extract those three sections from the matrix, but don't know how to code the loop
I'd like to extract those three sections from the matrix, but don't know how to code the loopk=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 Commento
Rik
il 2 Giu 2021
What is your intended output? A cell vector with one section in each cell element?
Risposta accettata
Più risposte (1)
KSSV
il 2 Giu 2021
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,:)
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!