Compare two matrices of different dimensions
32 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jonathan
il 17 Apr 2019
Commentato: Walter Roberson
il 30 Giu 2021
I have two matrices A=500000*4 and B=4300000*4.
Matrix A = id, plus 3 columns of NaN.
Matrix B = id, lat, lon, elev.
I would like to find the id values in A = B, and based on those same id values, fill matrix A with the missing lat,lon,elev.
The code I have is:
for i=1:length(A)
ind = B(:,1)==A(i,1);
A(i,2:4) = B(ind,2:4);
clear ind
end
This works but is extremely slow. How can I do this more efficiently?
Thanks
Jon
0 Commenti
Risposta accettata
Jonathan
il 17 Apr 2019
5 Commenti
Leonardo Monteiro de Carvalho
il 30 Giu 2021
If I wanted to compare C and D to check, for example, if C is in between 2 values (in 2 different columns) of array D, how would I do that?
Walter Roberson
il 30 Giu 2021
Two fixed columns? Or any two columns? If it is any two columns, then is D guaranteed to be non-decreasing along the rows? What is the desired output?
And to confirm, C and D are 2D and C and D have the same number of rows but not necessarily the same number of columns?
Or is C a column vector with the same number of entries that D has rows and the task is to find out which column the value is in relative to the content of that row of D?
Is there some pattern to the way D is arranged?
Più risposte (1)
darova
il 17 Apr 2019
ind = B(:,1)==A(i,1); % array of 5000000x1 logical
Try:
for i=1:length(A)
ind = find(B(:,1)==A(i,1),1,'first'); % one value
A(i,2:4) = B(ind,2:4);
% clear ind
end
1 Commento
Vedere anche
Categorie
Scopri di più su NaNs in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!