how to assign a number to a specific location.

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

As was mentioned in a previous thread, please post the code that produces a table, not the table itself.
Also, it is not clear to me what you mean. Is the vector you posted relevant to the task? Do you have a set of vectors (one for each row)?
i have posted the code previously and i havent got the reply. that's why i simplified the question.
yes i do have a set of vector for each row. In the vector of 0 and 1, 1 is present when table 1 and table 2 has same value
If they already have the same value, what is your question? You don't need to assign the value anymore, because they are already the same.
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
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)

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

Tags

Asked:

on 2 Aug 2019

Answered:

Rik
on 3 Aug 2019

Community Treasure Hunt

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

Start Hunting!