Finding equal values in two matrices and create a new matrix

11 ビュー (過去 30 日間)
Paul
Paul 2020 年 7 月 8 日
コメント済み: Paul 2020 年 7 月 14 日
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 件のコメント
Paul
Paul 2020 年 7 月 14 日
Hi David,
thank you, that's exactly what I wanted to have.

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by