How to improve my inefficient code?

1 次查看(过去 30 天)
Hi all,
I have a 21x288 table (attached).
I have a function that compares the variables and calculates the ICC (attached). For my purposes, I am running the following comparisons:
1 vs 2, 1 vs 3, 1 vs 4
5 vs 6, 5 vs 7, 5 vs 8
9 vs 10, 9 vs 11, 9 vs 12
and so on until the end of the table (numbers refers to columns)
I am planning to do so as below. Can this be done in a more efficient way?
load 'data'
data1=data(:,1:4:end); %get all data from data collection 1 into one table
data2=data(:,2:4:end); %get all data from data collection 2 into one table
data3=data(:,3:4:end); %get all data from data collection 3 into one table
data4=data(:,4:4:end); %get all data from data collection 4 into one table
%% Variable 1
% Comparison 1 (data collection 1 vs data collection 2)
p_acc_z_mean_1vs2=[data1(:,1),data2(:,1)];
p_acc_z_mean_1vs2 = table2array (p_acc_z_mean_1vs2);
[r_p_acc_z_mean_1vs2]=ICC(p_acc_z_mean_1vs2,'C-k',0.05,0.5);
% Comparison 2 (data collection 1 vs data collection 3)
p_acc_z_mean_1vs3=[data1(:,1),data3(:,1)];
p_acc_z_mean_1vs3 = table2array (p_acc_z_mean_1vs3);
[r_p_acc_z_mean_1vs3]=ICC(p_acc_z_mean_1vs3,'C-k',0.05,0.5);
% Comparison 3 (data collection 1 vs data collection 4)
p_acc_z_mean_1vs4=[data1(:,1),data4(:,1)];
p_acc_z_mean_1vs4 = table2array (p_acc_z_mean_1vs4);
[r_p_acc_z_mean_1vs4]=ICC(p_acc_z_mean_1vs4,'C-k',0.05,0.5);
%% Repeat the above for the remaining 71 variables
  4 个评论
Johan
Johan 2022-3-1
编辑:Johan 2022-3-1
Maybe this would work for you ?
%transform your table in an array
workdata = table2array(data);
for i = 1:4:size(workdata,2)
for j = 1:3
%apply your function to your data comparing i column to i+j columns
[tosave] = ICC([workdata(:,i),workdata(:,i+j)],'C-k',0.05,0.5);
%storing the results in a structure
results(i).r_p_acc_z_mean(j) = tosave';
end
end
Tomaszzz
Tomaszzz 2022-3-1
@Johan Pelloux-Prayer Thanks Johan. It seems to calcuate the output for all variables ale all comparisons as below. Can you post this as an answer please. Can the storing of the results be improved to get rid of empty rows?

请先登录,再进行评论。

采纳的回答

Johan
Johan 2022-3-1
编辑:Johan 2022-3-1
(modified from comment to get rid of empty rows)
Maybe this would work for you ?
%transform your table in an array
workdata = table2array(data);
k=1;
for i = 1:4:size(workdata,2)
for j = 1:3
%apply your function to your data comparing i column to i+j columns
[tosave] = ICC([workdata(:,i),workdata(:,i+j)],'C-k',0.05,0.5);
%storing the results in a structure
results(k).r_p_acc_z_mean(j) = tosave';
end
k=k+1;
end
  2 个评论
Tomaszzz
Tomaszzz 2022-3-1
@Johan Pelloux-Prayer Thanks Johan. This time every second and third row it adds a '0' to the output. Also, compared to the first version, it outputs more rows than expected (216 rows) while there should be 78. Any idea how to get rid of this as I am failing to correct this?
Johan
Johan 2022-3-1
Yes my bad, I put the k=k+1 statement in the wrong for loop, I edited the answer above to fix it

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2022-3-1
This is a first point to start from:
load 'data'
Calling load without catching the output creates variables dynamically. This can impede the JIT-acceleration massively can low down the code especially in loops. In addition it makes debugging much harder, because it is not visible in the code, if a specific name is a variable or a function.
Avoid hiding indices in the names of variables as in "p_acc_z_mean_1vs3". Use an array instead. Then you can call the variables in a loop using indices.
The JIT acceleration can suffer from changing the types of variables. Avoid to create a new table and convert it to an array using the same name:
p_acc_z_mean_1vs2 = [data1(:,1), data2(:,1)]; % Create 2 tables and join them
p_acc_z_mean_1vs2 = table2array (p_acc_z_mean_1vs2); % Expensive re-typing
A cheaper version:
p_acc_z_mean_1vs2 = data2{:,1:2}; % Numeric matrix in one step

Matt J
Matt J 2022-3-1
编辑:Matt J 2022-3-1
It would improve things modestly if you just convert all the data to an array from the get-go. It doesn't look like you're exploiting the features of table form in any way.
load 'data'
data=table2array(data);
data1=data(:,1:4:end); %get all data from data collection 1 into one table
data2=data(:,2:4:end); %get all data from data collection 2 into one table
data3=data(:,3:4:end); %get all data from data collection 3 into one table
data4=data(:,4:4:end); %get all data from data collection 4 into one table
However, I think the only significant gains that can happen is if you rewrite/vectorize ICC.m so that it processes many variables simultaneously, e.g.., to support syntax like this:
M=permute( cat(3,data1,data2) ,[1,3,2]);
out = ICC(M,'C-k',0.05,0.5);
  3 个评论
Matt J
Matt J 2022-3-1
Yes, because you haven't rewritten ICC.m ! You've just used it as if it is already ready to receive input in that form.
Tomaszzz
Tomaszzz 2022-3-1
Thanks Matt, I find the below very useful as I have another function which requires the input data to be structured in this way and I had no clue how to do this
M=permute( cat(3,data1,data2) ,[1,3,2]);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by