Add cell value from a matrix row based on corresponding index to another matrix row
Show older comments
I have two arrays A and B.
Column 1 of array A contain the index number of the samples; other columns of matrix A contain other attributes of each sample, (in array B, the first two cell along each row contain an attribute and the index number of a sample)
Columns 2, 4 and 6 of array B contain the index number of samples contained in matrix A.
I am trying to insert corresponding sample attribute in column 4 of array A (based on coresponding index number in array A and B) into new 3rd, 6th and 9th column of matrix B; to have matrix C as shown below:
A = [1,3.43,2.34,5.43,3.22;
2,8.32,6.34,7.34,2.34;
3,3.67,8.34,8.23,1.34;
4,2.67;6.89,4.99,8.65;
5,1.33,5.42,2.53,6.13]
B = [ 0.12,2,0.15,1,0.65,3;
0.33,5,0.62,4,0.55,1;
0.91,1,0.77,2,0.66,5]
C = [ 0.12,2,7.34,0.15,1,5.43,0.65,3,1.34;
0.33,5,2.53,0.62,4,4.99,0.55,1,5.43;
0.91,1,5.43,0.77,2,7.34,0.66,5,2.53]
I have tried the following code but got an error:
For i= 1:3
f=ismember(A(:,1(i)), B(:,2), 'rows');
k=find(f);
f=A(f,4);
C= [C,f(i)]
end
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!