Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

iterative search through matrix in order to add a column

1 visualizzazione (ultimi 30 giorni)
Elissa Moller
Elissa Moller il 29 Mag 2020
Chiuso: MATLAB Answer Bot il 20 Ago 2021
I have 2 matrices one of which is 27811x2 and the second of which is 254x2. I was hoping to search the first column of the first matrix for each value from the first column of the second matrix. There will be more than one match. For each match I want to create a third column in the first matrix with the corresponding value from the second column in the second matrix. Is there a way to do this? I was trying different versions of for loops with if statements but couldn't get it to work.

Risposte (1)

Star Strider
Star Strider il 29 Mag 2020
I have no clear idea what you want to do.
Try this:
First = [randi(50, 100, 1) rand(100,1)];
Second = [randi(50, 10, 1) rand(10, 1)];
T1 = table(First(:,1), First(:,2)); % ‘First Matrix’ Table
T2 = table(Second(:,1), Second(:,2)); % ‘Second Matrix’ Table
T3 = innerjoin(T1, T2, 'Keys','Var1'); % Joined Tables
.
  1 Commento
Elissa Moller
Elissa Moller il 29 Mag 2020
I ended up doing this
for i1=1:size(matrix1(:,1))
for i2=1:size(matrix2(:,1))
if matrix1(i1,1)==matrix2(i2,1)
matrix1(i1,3)=matrix2(i2,2);
end
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by