how to assign a number to a specific location.

5 views (last 30 days)
karishma koshy
karishma koshy on 2 Aug 2019
Answered: Rik on 3 Aug 2019
Hi All
result=
[0 0 1 0
0 0 0 0
0 0 0 0
0 0 0 0]
using 'find' I found the position of one in the above. obviously it is the 3rd position. Horizontal number of elements represents number of elements in TABLE_2 and Vertical number of elements represnts the number of elements in TABLE_1
TABLE_1
column value
2 1
4 2
6 3
8 4
how can i assign 1st value in the table ie 1 to second table 3rd position as the both have same column value.
TABLE_2
column value
10 5
12 6
2 1
14 7
Thank you
  5 Comments
karishma koshy
karishma koshy on 3 Aug 2019
Table 1 is already assigned as 1:4. Then I am comparing table to table 2. When compared, the common number 2 in both table needs to have same value 1. How can I assign 1 to 3rd position
Rik
Rik on 3 Aug 2019
So originally table 2 had another value in that position? The third position of table2.value is already a 1.

Sign in to comment.

Answers (1)

Rik
Rik on 3 Aug 2019
I'm going to assume you want to treat table 1 as your lookup table, where you want matching column indices to have matching values. To better show the effect of my proposed solution I have edited both tables. I also assumed you used implicit expansion to get your logical array. For larger tables I would suggest using the ismember method in the commented code.
t1=table([2 4 6 8 14]',[1 2 3 4 5]','VariableNames',{'column','value'});
t2=table([10 12 2 14]',[5 6 999 7]','VariableNames',{'column','value'});
%This method might cause issues when using very large arrays. In those
%cases the commented code below might be a better choice.
%[a,b]=ismember(t2.column,t1.column);
%t1_ind=b(a);t2_ind=find(a);
if verLessThan('matlab','9.1')
matchgrid=bsxfun(@eq,t1.column,t2.column');
else
matchgrid=t1.column==t2.column';
end
[t1_ind,t2_ind]=find(matchgrid);
t2_new=t2;
t2_new.value(t2_ind)=t1.value(t1_ind);
clc
disp('t1:')
disp(t1)
disp('t2 (old):')
disp(t2)
disp('t2 (new):')
disp(t2_new)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!