Finding equal values in two matrices and create a new matrix

5 次查看(过去 30 天)
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

采纳的回答

David Hill
David Hill 2020-7-8
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 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by