How do I loop through a matrix and break it up into distinct sections?
Mostra commenti meno recenti
I have a set of data that is a 510x3 matrix in the form of :
[2.000000 56 0.000000
4.000000 5.267480 2
4.500000 6.979411 2
5.000000 8.427968 2
5.500000 9.744838 2
6.000000 11.193396 2
6.500000 12.378578 2
7.000000 13.168700 2
7.500000 14.748944 2
8.000000 15.407379 2
8.500000 15.670753 2
9.000000 16.855936 2
9.500000 17.382685 2
10.000000 19.753050 2 ...]
Every 56 lines of this data is a line that includes the number 55 OR 56 in column 2. I am trying to loop through this matrix and break it up into distinct separate sections marked by these lines that include the number 55/56. My ultimate objective is essentially to reformat this data into a form that utilizes indices positions instead of real world horizontal distances, and this will require several nested loops to read through the data section by section. I am relatively new to programming and coding in this manner, so any tips would be incredibly helpful!
This is my starting point:
% line to read in data from file
j=zeros(3,length(data));
for i=1:length(data)
if data(i,2)== 55 || 56
j=data(i,:);
i=i+1;
end
end
EDIT: Thank you for the responses! I didn't want to just leave this question open forever with no updates, but I did actually find a python script that someone had made to do the exact thing I'm trying to do (converting data from one software format to another)!
1 Commento
Dustin
il 25 Feb 2024
Risposta accettata
Più risposte (1)
Adrián László Szemenyei
il 26 Feb 2024
Modificato: Walter Roberson
il 26 Feb 2024
A=randi(60,510,3);%simulate your data
%A(:,2)==55|A(:,2)==56 index of rows where value is 55 or 56
b=[1:size(A,1)]'.*(A(:,2)==55|A(:,2)==56);%get the indices of such rows
indxs=[b(b~=0);size(A,1)];%one can alternatively use the find function also
indx_differences=[indxs(1);diff(indxs)];
my_data=mat2cell(A,indx_differences);%in documentation for mat2cell, rowDist=indx_differences
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!