Finding a row in a matrix that is closest to two separate conditions
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm familiar with using:
min(abs(1-x))
to find the value closest to whatever "x" is when specifying a target or condition. However, I'm trying to evaluate a matrix where a real part and imaginary part (each in an individual column) are the closest to a target value for each. An example snippet of my matrix would look like
%Delay Voltage Length Frequency Real Imaginary
0.3500 1.1500 0.1429 20.0000 0.9678 -2.2574
0.4000 1.1000 0.1429 20.0000 0.9181 -2.5046
0.4500 1.0000 0.1429 20.0000 0.9296 -3.0746
0.4500 1.0500 0.1429 20.0000 0.9342 -2.7859
0.4500 1.1000 0.1429 20.0000 1.0023 -2.5453
0.3500 1.1500 0.1429 21.2000 0.9369 -2.2832
0.4000 1.1000 0.1429 21.2000 0.9022 -2.5270
0.4000 1.1500 0.1429 21.2000 1.0067 -2.3389
In reality, I have around 800 rows that I have sorted by the Frequency column. I've already written:
freqVector = COMSOL_raw(1:50,freqColumn); % The number of frequencies found
for eachf = 1:length(freqVector)
eachChunk = COMSOL_sorted_RealImag((COMSOL_sorted_RealImag(:,freqColumn(1))==freqVector(1)),:)
end
eachChunk finds all the rows that match each Frequency and separates them into its own "chunk," if you will. From this, I want to add a line that will find which row has the closest "Real" value to 1.00 and also the closest "Imaginary" value to -3.14, and spit out the singular row that comes closest to matching those two simultaneous conditions. Thus far, my best efforts have been the following lines:
% ***Within The For Loop***
best(eachf,:) = eachChunk(:,min(abs(1 - eachChunk(:,realColumn)))) %Returns an position 2 index error
%OR
best = min(abs(1 - eachChunk(:,realColumn))) & min(abs(-3.14 - eachChunk(:,imagColumn)))% Returns Boolean 1/True
For the first line, last night I could get MATLAB to output the whole line that came closest for one condition (but i deleted it and can't figure out how to do it again). For the second line, I know it finds something to satisfy both conditions, but I can't tell if it's doing it for the array or if it is actually finding one line that satisfies both. And if it's the latter, I very much need help writing a line that will output the whole line from the array eachChunk. Thank you!
EDIT: I also attempted to create a complex number from the last two columns and evaluate the closest complex number to a single complex condition (i.e. 1 - 3.14i) but I couldn't get that to work either.
1 commentaire
Image Analyst
le 27 Sep 2020
What if one criteria points to one line, and the other criteria points to a different line? Which do you pick? You have to define a "cost function" that somehow uses the two distances to come up with one number that you can use to determine "closeness".
Réponses (1)
Bruno Luong
le 27 Sep 2020
Modifié(e) : Bruno Luong
le 27 Sep 2020
Assuming the data is in yourarr
rx = yourarr(:,5);
iy = yourarr(:,6);
z = rx + 1i*iy;
ztarget = 1;
[absdz, locmin] = min(abs(ztarget-z))
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display 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!