Extract rows from 2 different matrices with different dimensions using If statement
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi guys. Hoping that you're all ok.
For my project i need to every equal runID (column 5 on both matrices) to extract the rows that contains the higher value of the energy (column 3 on both matrices) in the same eventID (column 2 on both matrices).
Example, for a runID == 0, e need to extract the higher value of the energy on both matrices in the same eventID == 6. Using the .txt files i've attached, that corresponds to the first row in both matrices.
This is my current code:
for i=[1:99]
file_number=sprintf('%d',i);
R = fileread(strcat('right',num2str(file_number),'.txt'));
R(R == '*') = '';
AR = cell2mat( textscan(R, '%f %f %f %f %f %f', 'headerlines', 4));
L = fileread(strcat('left',num2str(file_number),'.txt'));
L(L == '*') = '';
AL = cell2mat( textscan(L, '%f %f %f %f %f %f', 'headerlines',4));
for n=[0:max(AR(:,5))];
if AR(:,5)==n;
if AL(:,5)==n;
idx=find(AR(:,2)==AL(:,2))
end
end
end
end
I've already tried different ways to exctrat what i need but i found always a different problem every time.
Thanks in advance.
0 commentaires
Réponse acceptée
Jan
le 24 Nov 2020
Modifié(e) : Jan
le 24 Nov 2020
if AR(:,5)==n;
This is a vector in the condition. Therefore Matlab inserts an all() implicitly, because conditions of if statements must be scalar.
I guess you want:
indexR = (AR(:, 5) == n);
ARn = AR(inbdexR, 2);
I do not completely understand, which problem you want to solve and what you want as output.
A hint: file_number is a string already, so omit num2str. Replace
file_number=sprintf('%d',i);
R = fileread(strcat('right',num2str(file_number),'.txt'));
by the simpler:
file = sprintf('right%d.txt', i);
R = fileread(file);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Call C from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!