Prev: input
Next: reassigning values of elements in matrix
From: Gerardo on 16 Jul 2010 02:36 Apologies for the basic question, but as a fairly new user I just cannot figure this out. I have two tables with thousands of rows, for both the first column is time. Some of the times (rows) in the second table are also present in the first table. I need to find those rows and append the values as new columns in the first table. As an example: A=[1 10 100; 2 20 200; 3 30 300; 4 40 400; ...]; B=[1.5 15 150; 2 0 20000; 3.5 0 35000; 4 0 40000; ...]; Resulting table= C=[1 10 100 NaN; 2 20 200 20000; 3 30 300 NaN; 4 40 400 40000; ...]; I tried using "ismember", and works fine when Size(Table1)>Size(Table2). When Table2 has more rows, it doesn't work. I also tried splitting a longer Table2 in two tables so they are smaller than Table1, but still didn't work, meaning that it is not consistent. I will really appreciate any comments, suggestions. Thanks, Antony
From: Oleg on 16 Jul 2010 03:26 > I tried using "ismember", and works fine when Size(Table1)>Size(Table2). When Table2 has more rows, it doesn't work. > I also tried splitting a longer Table2 in two tables so they are smaller than Table1, but still didn't work, meaning that it is not consistent. > > I will really appreciate any comments, suggestions. > > Thanks, > > Antony I modified slightly A and B so that the rows in B are more than in A. A=[1 10 100; 2 20 200; 3 30 300; 4 40 400]; B=[ 1 1 20 % added row 1.5 15 150; 2 0 20000; 3.5 0 35000; 4 0 40000;] % Preallocate A = [A, NaN(size(A,1),1)]; % Use ismember [tf,loc] = ismember(B(:,1),A(:,1)); A(loc(loc~=0),4) = B(tf,3) Oleg
From: Gerardo on 16 Jul 2010 04:15 "Oleg " <oleg.komarovRemoveThis(a)hotmail.it> wrote in message <i1p1id$6l6$1(a)fred.mathworks.com>... > > I tried using "ismember", and works fine when Size(Table1)>Size(Table2). When Table2 has more rows, it doesn't work. > > I also tried splitting a longer Table2 in two tables so they are smaller than Table1, but still didn't work, meaning that it is not consistent. > > > > I will really appreciate any comments, suggestions. > > > > Thanks, > > > > Antony > > I modified slightly A and B so that the rows in B are more than in A. > > A=[1 10 100; > 2 20 200; > 3 30 300; > 4 40 400]; > > B=[ 1 1 20 % added row > 1.5 15 150; > 2 0 20000; > 3.5 0 35000; > 4 0 40000;] > > % Preallocate > A = [A, NaN(size(A,1),1)]; > > % Use ismember > [tf,loc] = ismember(B(:,1),A(:,1)); > > A(loc(loc~=0),4) = B(tf,3) > > Oleg Oleg, Thanks a million. It works perfectly. Antony
|
Pages: 1 Prev: input Next: reassigning values of elements in matrix |