How to replace values of a large array by the values of a smaller one?
1 view (last 30 days)
Show older comments
I have two tables T1 and T2 with identical number of columns but different number of raw.
The x,y,z columns from both tables were created as below:
clear all;
close all;
XSTART=5;
XEND=20;
YSTART=5;
YEND=15;
STEP1=1;
STEP2=5;
%******************* CREATE TABLE1 ********************
xvector1 = XSTART:STEP1:XEND;
yvector1 = YSTART:STEP1:YEND;
numX1 = numel(xvector1);
numY1 = numel(yvector1);
yvector1 = repmat(yvector1(:),numX1,1);
xvector1 = repmat(xvector1 ,numY1,1);
COORDINATES1= [xvector1(:) yvector1];
VALUE1=(1:1:numX1*numY1)';
VALUE1(1:end,1)=10;
T1=[COORDINATES1 VALUE1];
%******************* CREATE TABLE2 ********************
xvector2 = XSTART:STEP2:XEND;
yvector2 = YSTART:STEP2:YEND;
numX2 = numel(xvector2);
numY2 = numel(yvector2);
yvector2 = repmat(yvector2(:),numX2,1);
xvector2 = repmat(xvector2 ,numY2,1);
COORDINATES2= [xvector2(:) yvector2];
VALUE2 =(1:numX2*numY2)';
T2=[COORDINATES2 VALUE2];
Now I want to create a table T3 replacing the values of the third column from T1 by the values of the third column from T2 with the same x,y- coordinate.
The expected T3 after proper substitution will have the following components:
5 5 1
5 6 10
5 7 10
5 8 10
5 9 10
5 10 2
5 11 10
5 12 10
5 13 10
5 14 10
5 15 3
6 5 10
6 6 10
6 7 10
6 8 10
6 9 10
6 10 10
6 11 10
6 12 10
6 13 10
6 14 10
6 15 10
7 5 10
7 6 10
7 7 10
7 8 10
7 9 10
7 10 10
7 11 10
7 12 10
7 13 10
7 14 10
7 15 10
8 5 10
8 6 10
8 7 10
8 8 10
8 9 10
8 10 10
8 11 10
8 12 10
8 13 10
8 14 10
8 15 10
9 5 10
9 6 10
9 7 10
9 8 10
9 9 10
9 10 10
9 11 10
9 12 10
9 13 10
9 14 10
9 15 10
10 5 4
10 6 10
10 7 10
10 8 10
10 9 10
10 10 5
10 11 10
10 12 10
10 13 10
10 14 10
10 15 6
and so on.
I hope someone knows how to do it.
Thanks in advance for your help
Emerson
0 Comments
Accepted Answer
Daniel Shub
on 13 Nov 2011
Is this what you mean:
T3 = T1;
[c, ia, ib] = intersect(T2(:, 1:2), T3(:, 1:2), 'rows');
T3(ib, 3) = T2(ia, 3)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!