Performing computation using contents stored in a table

1 vue (au cours des 30 derniers jours)
Deepa Maheshvare
Deepa Maheshvare le 24 Fév 2020
Commenté : darova le 27 Fév 2020
Hi,
I've the following table
t = [1 2 3 4 5 6 7 8 9]';
h = [2 3 4 5 6 7 8 9 10]';
value = [1 2 1 2 1 2 1 2 1]';
tbl = table(t,h,value);
tbl = mergevars(tbl,[1 2]);
I'm filtering the contents of the table using a search value. The contents present in the resulting table is used to perform some operations.
searchvalues = 1:10;
tic
for i = 1:length(searchvalues)
searchval = searchvalues(i);
newtable = tbl(any(tbl.Var1 == searchval, 2), :)
tochange = newtable.Var1(:, 2) == searchval;
newtable.Var1(tochange,:) = fliplr(newtable.Var1(tochange,:));
result(i) = sum((newtable.Var1(:,1) - newtable.Var1(:,2)).*newtable.value);
end
toc
result
I'd like to ask for suggestions on how to speed up the steps carried out in for loop.
I tried the following,
searchvalues = 1:10;
tic
i = searchvalues;
newtable = tbl(any(tbl.Var1 == i, 2), :)
tochange = newtable.Var1(:, 2) == i;
newtable.Var1(tochange,:) = fliplr(newtable.Var1(tochange,:));
result(i) = sum((newtable.Var1(:,1) - newtable.Var1(:,2)).*newtable.value);
end
toc
result
But, this didn't work.
EDIT: what happens in the for loop?
The following contents are stored in a table, in variable 'tbl',
Var1 value
______________
1 2 1
2 3 2
3 4 1
4 5 2
I would like to do the following,
i = 2,
If the variable i is present in tbl.Var1, I would like to retain only those rows and delete the remining rows.
Example,
Var1 value
______________
1 2 1
2 3 2
Also, if i is present in column 2 of Multico, the value in column 2 of tbl.Var1 , the columns are flipped
Th expected output is,
Var1 value
______________
2 1 1
2 3 2
Then, I substract tbl(:,1) - tbl(:,2) , this results in a column vector. Sum of the dot product of this column vector and tbl.value is obtained, stored in variabl result.
I repeat the same for different values of i.
  4 commentaires
darova
darova le 25 Fév 2020
Yes, it's clear now. Thank you
Do you always operate on numbers? Why do you need table?
Deepa Maheshvare
Deepa Maheshvare le 26 Fév 2020
Modifié(e) : Deepa Maheshvare le 26 Fév 2020
Yes, I always operate on numbers . The data comes from an excel file, so I have imported as table. I could store in other formats if that will help in speeding up the opeartions carried out in for loop.

Connectez-vous pour commenter.

Réponses (1)

darova
darova le 26 Fév 2020
Version without tables
var11 = [1 2 3 4 5 6 7 8 9]';
var12 = [2 3 4 5 6 7 8 9 10]';
value = [1 2 1 2 1 2 1 2 1]';
i = 2;
ind = (var11==i) | (var12==i);
% (without replacing columns) tbl(:,1) - tbl(:,2) the same as
result = sum( (2*i-var11-var12).*value.*ind );
  2 commentaires
Deepa Maheshvare
Deepa Maheshvare le 27 Fév 2020
Hi darova,
Thanks a lot for precisely answering my question. I'd like to know how the same can be implemented for anothe test case.
Instead of doing
newtable.Var1(:,1) - newtable.Var1(:,2)
I have to use newtable.Var1(:,1) and newtable.Var1(:,2) as indices to perform the same operation on a variable.
i.e.
A(newtable.Var1(:,1)) - A(newtable.Var1(:,2))
here A is a column vector with numbers in it.
In this case, I'll not be able to do (2*i-var11-var12) .
Could you please suggest other alternatives?
darova
darova le 27 Fév 2020
Try
idx = abs( newtable.Var1(:,1)) - A(newtable.Var1(:,2) );
A(ix)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by