Hello, this is my first post here. Currently I am working on a script that should recalculate the column numbers as following:
firstvalue = table{1,1};
i = 1;
height = height(table);
for i = 1:height
x = table{i,1};
table{i,1} = (x-firstvalue)/fs*1000;
i = i+1;
end
This loop does what I want, but is terribly slow. Is there a way to make this loop execute faster? The loop works in the table with around 50.000 rows. I think the loop could be more efficient but don't know how.
Thank you!

댓글 수: 3

Adam
Adam 2017년 1월 18일
편집: Adam 2017년 1월 18일
What is 'table'? Is it a Table object? I'm not familiar with tables, but if it is a table then I would imagine it is quicker to calculate all your results into a numeric array first (extract all the first column of the table at once) and then assign the whole thing to the table in one go rather than updating each row of the table directly. Not sure if this is possible with tables though since I don't use them. It is a usual Matlab strategy for objects such as uitable though.
Jan
Jan 2017년 1월 18일
Omit the "i=i+1". It is only confusing inside a loop. A corresponding warning should appear in the editor.
Daniel van Dijk
Daniel van Dijk 2017년 1월 18일
Sorry i was trying to simplify but made it confusing. I updated the first post.
Sounds logical. I dont know what kind of table it is, to me they are all the same. I'll take a look!

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

 채택된 답변

Akira Agata
Akira Agata 2017년 2월 18일

1 개 추천

How about doing this.
table{:,1} = (table{:,1}-table{1,1})/fs*1000;
And let's check the calculation time.
fs = 100;
table1 = array2table(rand(1000,2));
table2 = table1;
% Slightly modified original code
tic
firstvalue = table1{1,1};
i = 1;
n = height(table1);
for i = 1:n
x = table1{i,1};
table1{i,1} = (x-firstvalue)/fs*1000;
end
toc
% Proposed code
tic
table2{:,1} = (table2{:,1}-table2{1,1})/fs*1000;
toc
% Check the answer
isequal(table1, table2)
In my PC, the result is 0.131066 sec -> 0.003073 sec and table2 is the same as table1.

추가 답변 (0개)

카테고리

제품

태그

질문:

2017년 1월 18일

답변:

2017년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by