Extract rows from 2 different matrices with different dimensions using If statement

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Jan
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);
  1 commentaire
David Martins
David Martins le 24 Nov 2020
indexR = (AR(:, 5) == n);
ARn = AR(inbdexR, 2);
This is brilliant! It does exactly what i want it to do as a first step. Your explanation about the if statements is the true hero here. I was not aware of that.
What i really want is to have the entire row witch contain the higer energy value (column 3) from both matrices in each eventID (column 2). Some files have different eventID within the same runID. Example
ARn=[6710 0 0.00218000000000000 7 431 2203.10470000000
6711 0 7.49900000000000e-06 8 431 2203.10470000000
6712 2 0.00217740000000000 9 431 2203.10860000000
6713 2 2.49900000000000e-06 10 431 2203.10860000000
6714 2 7.49900000000000e-06 8 431 2203.10860000000]
ALn=[11892 0 0.00701500000000000 7 431 2203.10470000000
11893 0 4.24900000000000e-05 6 431 2203.10470000000
11894 0 3.99900000000000e-05 8 431 2203.10470000000
11895 0 4.99900000000000e-06 5 431 2203.10470000000
11896 0 2.49900000000000e-06 9 431 2203.10470000000
11897 2 0.00592990000000000 9 431 2203.10860000000
11898 2 1.99900000000000e-05 10 431 2203.10860000000
11899 2 1.99900000000000e-05 8 431 2203.10860000000]
In this example, for a runID == 0, i want to extract the first from both matrices and for runID == 2, extract the third row from ARn and the sixth eow from ALn.

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by