Finding equal values in two matrices and create a new matrix

8 visualizaciones (últimos 30 días)
Paul
Paul el 8 de Jul. de 2020
Comentada: Paul el 14 de Jul. de 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

Respuesta aceptada

David Hill
David Hill el 8 de Jul. de 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

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by