How do I loop through a matrix and break it up into distinct sections?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dustin
el 25 de Feb. de 2024
Editada: Walter Roberson
el 26 de Feb. de 2024
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)!
Respuesta aceptada
Walter Roberson
el 25 de Feb. de 2024
Editada: Walter Roberson
el 25 de Feb. de 2024
mask = ismember(YourData(:,2), [55 56]) .'; %row vector result
starts = strfind([0 mask], [0 1]);
stops = [starts(2:end)-1, size(YourData,1)];
The data blocks are then
for K = 1 : numel(starts)
blocks{K} = data(YourData(starts(K):stops(K), :));
end
0 comentarios
Más respuestas (1)
Adrián László Szemenyei
el 26 de Feb. de 2024
Editada: Walter Roberson
el 26 de Feb. de 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
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!