Performing computation using contents stored in a table

조회 수: 1 (최근 30일)
Deepa Maheshvare
Deepa Maheshvare 2020년 2월 24일
댓글: darova 2020년 2월 27일
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
darova
darova 2020년 2월 25일
Yes, it's clear now. Thank you
Do you always operate on numbers? Why do you need table?
Deepa Maheshvare
Deepa Maheshvare 2020년 2월 26일
편집: Deepa Maheshvare 2020년 2월 26일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

darova
darova 2020년 2월 26일
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
Deepa Maheshvare
Deepa Maheshvare 2020년 2월 27일
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 2020년 2월 27일
Try
idx = abs( newtable.Var1(:,1)) - A(newtable.Var1(:,2) );
A(ix)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by