Reputation: 901
I am working with two tables in MATLAB:
A1_Table [m*r]
A2_Table [m*q]
where the tables have different number of columns but similar number of rows as you can see and it follows that r>q
. In fact A2_Table
is a sub-sample of A1_Table
, meaning all the matrix entries in A1_Table
can be found in A2_Table
as well.
Associated with each column is a VariableName and with each row a RowName. What I want to do is to set all the entries in A1_Table
to zero for which A2_Table
has a value greater than zero. Here is what I perform:
[r,c] = find(table2array(A2_Table)) ;
for i = 1 : length(r)
A1_Table(A2_Table.Properties.RowNames(r(i)),A2_Table.Properties.VariableNames(c(i))) = {0} ;
end
As you can see I am accessing the corresponding entries in A1_Table
through the rows and column names (keywords). This process works but it is very slow as there exists 2 million values for which the loop needs to run. Any idea to accelerate this process? Thanks
Upvotes: 0
Views: 44
Reputation: 2864
Here's one way. It seems to scale well:
clear all;
clc;
m=3;
r=6;
q=3;
A1_Table = randi(10,m,r);
A2_Table = randi([0,1],m,q);
Zero_Table = zeros(m,q);
A1_Table
% remove extra columns from A1_Table
A1_Table(:,q+1:r) = [];
A2_Table
% compare A2 table to zeros table
compArr = A2_Table <= Zero_Table;
% compare compArr matrix to A1_Table
result = A1_Table.* compArr;
result
Upvotes: 1