How to improve my inefficient code?

2 visualizzazioni (ultimi 30 giorni)
Tomaszzz
Tomaszzz il 1 Mar 2022
Commentato: Tomaszzz il 1 Mar 2022
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 Commenti
Johan
Johan il 1 Mar 2022
Modificato: Johan il 1 Mar 2022
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 il 1 Mar 2022
@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?

Accedi per commentare.

Risposta accettata

Johan
Johan il 1 Mar 2022
Modificato: Johan il 1 Mar 2022
(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 Commenti
Tomaszzz
Tomaszzz il 1 Mar 2022
@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 il 1 Mar 2022
Yes my bad, I put the k=k+1 statement in the wrong for loop, I edited the answer above to fix it

Accedi per commentare.

Più risposte (2)

Jan
Jan il 1 Mar 2022
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 il 1 Mar 2022
Modificato: Matt J il 1 Mar 2022
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 Commenti
Matt J
Matt J il 1 Mar 2022
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 il 1 Mar 2022
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]);

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by