Finding equal values in two matrices and create a new matrix
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
how can I find equal values in two matrices and create a new matrix?
More precise:
I have two .txt files 'Particles' and 'Fragments' (see attachement).
I want to assign the IDs of the Fragments (column 1 in 'Fragments.txt') to the cartesian coordinates of the respective particle from which they emerged from (columns 9-11 in 'Particles.txt') by finding the motherID of the particles (column 2 in 'Particles.txt') in the 'Fragments.txt' (column 6 in 'Fragments.txt').
In addition to that I want to create a matrix that contains the fragment IDs in the first column and the respective coordinates of the Particles from which their emerged from in columns 2-4.
My first attempts:
clear all
clc
A1 = importdata('Particles.txt');
A2 = importdata('Fragments.txt');
ID_Particles = A1(:,2);
MID_Fragments = A2(:,6);
CooParticles = A1(:,9:11);
[a,b] = ismember(MID_Fragments,CooParticles,'rows');
% c = ismember(ID_Particles,MID_Fragments,'rows');
% I = find(c);
%
% if ismember(ID_Particles,MID_Fragments)
%
% disp(CooParticles)
%
% end
Réponse acceptée
David Hill
le 8 Juil 2020
A1 = importdata('Particles.txt');
A2 = importdata('Fragments.txt');
ID_Particles = A1(:,2);
MID_Fragments = A2(:,6);
CooParticles = A1(:,9:11);
newMatrix=zeros(size(A2,1),4);
newMatrix(:,1)=A2(:,1);
for k=1:length(ID_Particles)
a=MID_Fragments==ID_Particles(k);
newMatrix(a,2:4)=repmat(CooParticles(k,:),nnz(a),1);
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices 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!