Finding equal values in two matrices and create a new matrix

5 vues (au cours des 30 derniers jours)
Paul
Paul le 8 Juil 2020
Commenté : Paul le 14 Juil 2020
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
  1 commentaire
Paul
Paul le 14 Juil 2020
Hi David,
thank you, that's exactly what I wanted to have.

Connectez-vous pour commenter.

Réponse acceptée

David Hill
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

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays 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