Help with a for-loop of an array of iterative matrices

1 visualización (últimos 30 días)
Spyryx Biosciences
Spyryx Biosciences el 4 de Mayo de 2017
Editada: Stephen23 el 5 de Mayo de 2017
I have written a for-loop that extracts data from Excel files and then steps through the array of matrices that are named serially but in the most inefficient way possible. The end result is the combination of all the data but in the right configuration for downstream analysis. I am still new to MATLAB and would greatly appreciate some suggestions on how to include more for-loops in my code so that I can apply similar concepts in the future.
Here is the code:
for j=1:4
rep1 = xlsread( [num2str(j) '.xlsx'] , 'B9:M14');
rep2 = xlsread( [num2str(j) '.xlsx'] , 'B25:M30');
rep3 = xlsread( [num2str(j) '.xlsx'] , 'B41:M46');
rep4 = xlsread( [num2str(j) '.xlsx'] , 'B57:M62');
rep5 = xlsread( [num2str(j) '.xlsx'] , 'B73:M78');
rep6 = xlsread( [num2str(j) '.xlsx'] , 'B89:M94');
rep7 = xlsread( [num2str(j) '.xlsx'] , 'B105:M110');
rep8 = xlsread( [num2str(j) '.xlsx'] , 'B121:M126');
rep9 = xlsread( [num2str(j) '.xlsx'] , 'B137:M142');
rep10 = xlsread( [num2str(j) '.xlsx'] , 'B153:M158');
col1 = rep1(:,1)';
col2 = rep1(:,2)';
col3 = rep1(:,3)';
col4 = rep1(:,4)';
col5 = rep1(:,5)';
col6 = rep1(:,6)';
col7 = rep1(:,7)';
col8 = rep1(:,8)';
col9 = rep1(:,9)';
col10 = rep1(:,10)';
col11 = rep1(:,11)';
col12 = rep1(:,12)';
rep1 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep1 = rep1';
col1 = rep2(:,1)';
col2 = rep2(:,2)';
col3 = rep2(:,3)';
col4 = rep2(:,4)';
col5 = rep2(:,5)';
col6 = rep2(:,6)';
col7 = rep2(:,7)';
col8 = rep2(:,8)';
col9 = rep2(:,9)';
col10 = rep2(:,10)';
col11 = rep2(:,11)';
col12 = rep2(:,12)';
rep2 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep2 = rep2';
col1 = rep3(:,1)';
col2 = rep3(:,2)';
col3 = rep3(:,3)';
col4 = rep3(:,4)';
col5 = rep3(:,5)';
col6 = rep3(:,6)';
col7 = rep3(:,7)';
col8 = rep3(:,8)';
col9 = rep3(:,9)';
col10 = rep3(:,10)';
col11 = rep3(:,11)';
col12 = rep3(:,12)';
rep3 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep3 = rep3';
col1 = rep4(:,1)';
col2 = rep4(:,2)';
col3 = rep4(:,3)';
col4 = rep4(:,4)';
col5 = rep4(:,5)';
col6 = rep4(:,6)';
col7 = rep4(:,7)';
col8 = rep4(:,8)';
col9 = rep4(:,9)';
col10 = rep4(:,10)';
col11 = rep4(:,11)';
col12 = rep4(:,12)';
rep4 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep4 = rep4';
col1 = rep5(:,1)';
col2 = rep5(:,2)';
col3 = rep5(:,3)';
col4 = rep5(:,4)';
col5 = rep5(:,5)';
col6 = rep5(:,6)';
col7 = rep5(:,7)';
col8 = rep5(:,8)';
col9 = rep5(:,9)';
col10 = rep5(:,10)';
col11 = rep5(:,11)';
col12 = rep5(:,12)';
rep5 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep5 = rep5';
col1 = rep6(:,1)';
col2 = rep6(:,2)';
col3 = rep6(:,3)';
col4 = rep6(:,4)';
col5 = rep6(:,5)';
col6 = rep6(:,6)';
col7 = rep6(:,7)';
col8 = rep6(:,8)';
col9 = rep6(:,9)';
col10 = rep6(:,10)';
col11 = rep6(:,11)';
col12 = rep6(:,12)';
rep6 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep6 = rep6';
col1 = rep7(:,1)';
col2 = rep7(:,2)';
col3 = rep7(:,3)';
col4 = rep7(:,4)';
col5 = rep7(:,5)';
col6 = rep7(:,6)';
col7 = rep7(:,7)';
col8 = rep7(:,8)';
col9 = rep7(:,9)';
col10 = rep7(:,10)';
col11 = rep7(:,11)';
col12 = rep7(:,12)';
rep7 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep7 = rep7';
col1 = rep8(:,1)';
col2 = rep8(:,2)';
col3 = rep8(:,3)';
col4 = rep8(:,4)';
col5 = rep8(:,5)';
col6 = rep8(:,6)';
col7 = rep8(:,7)';
col8 = rep8(:,8)';
col9 = rep8(:,9)';
col10 = rep8(:,10)';
col11 = rep8(:,11)';
col12 = rep8(:,12)';
rep8 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep8 = rep8';
col1 = rep9(:,1)';
col2 = rep9(:,2)';
col3 = rep9(:,3)';
col4 = rep9(:,4)';
col5 = rep9(:,5)';
col6 = rep9(:,6)';
col7 = rep9(:,7)';
col8 = rep9(:,8)';
col9 = rep9(:,9)';
col10 = rep9(:,10)';
col11 = rep9(:,11)';
col12 = rep9(:,12)';
rep9 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep9 = rep9';
col1 = rep10(:,1)';
col2 = rep10(:,2)';
col3 = rep10(:,3)';
col4 = rep10(:,4)';
col5 = rep10(:,5)';
col6 = rep10(:,6)';
col7 = rep10(:,7)';
col8 = rep10(:,8)';
col9 = rep10(:,9)';
col10 = rep10(:,10)';
col11 = rep10(:,11)';
col12 = rep10(:,12)';
rep10 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep10 = rep10';
experiment = [rep1(:)';rep2(:)';rep3(:)';rep4(:)';rep5(:)';rep6(:)';rep7(:)';rep8(:)';rep9(:)';rep10(:)'];
xlswrite( ['experiment' num2str(j) '.xlsx'] , experiment);
end
I apologize in advance for the repetitive nature and the unnecessary length of the code but this is was only way I knew how to write it. Thanks again for all your help.
  2 comentarios
John D'Errico
John D'Errico el 4 de Mayo de 2017
Then the answer is to learn to use vectors and arrays, instead of numbered variables as you have done.
Instead of assigning a variable called rep1, rep2, rep3 (etc.), create an array. Then assign the rows or columns of that array.
Learn to use MATLAB as it was designed. USE MATRICES.
Stephen23
Stephen23 el 5 de Mayo de 2017
Editada: Stephen23 el 5 de Mayo de 2017
"and then steps through the array of matrices that are named serially but in the most inefficient way possible"
When you write lots of numbered available then you are writing inefficient code. The name "MATLAB" comes from "MATrix LABoratory", and not from "lets split the data into lots of separate numbered variables". To use MATLAB efficiently just keep your data in vectors/matrices/arrays (not only is it more efficient it is simpler, neater, easier to debug, easier to write, easier to understand,...).
Computers are good at one thing: repeatedly doing simple tasks. So when you copy-and-paste code like that your are doing the computer's work for it. Why waste your life doing what the computer can do faster and better than you? Use a loop and indexing, or vectorized code, and get your computer to do the work.
Note that putting any meta-data into a variable names is a bad idea, not just pseudo-indices:

Iniciar sesión para comentar.

Respuestas (3)

Jan
Jan el 5 de Mayo de 2017
Editada: Jan el 5 de Mayo de 2017
Hiding indices in the name of a variable is a bad idea. See http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for explanations.
This:
col1 = rep1(:,1)';
col2 = rep1(:,2)';
col3 = rep1(:,3)';
col4 = rep1(:,4)';
col5 = rep1(:,5)';
col6 = rep1(:,6)';
col7 = rep1(:,7)';
col8 = rep1(:,8)';
col9 = rep1(:,9)';
col10 = rep1(:,10)';
col11 = rep1(:,11)';
col12 = rep1(:,12)';
rep1 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep1 = rep1';
can be simplified to:
rep1 = rep1(:, 1:12);
But if rep1 contains the 12 columns 'B9:M14' only, there is no reason to crop the data at all and you can omit this code completely. Now let's use a loop to import the different blocks of 6 columns and store the blocks in a cell:
rep = cell(1, 10);
index = 25:16:153;
for iFile = 1:4
File = [num2str(iFile) '.xlsx'];
for iBlock = 1:10
Range = sprintf('B%d:M%d', index(iBlock), index(iBlock) + 5);
Data = xlsread(File, Range);
rep{iBlock} = Data(:).';
end
experiment = cat(1, rep{:});
xlswrite(sprintf('experiment%d.xlsx', iFile), experiment);
end

Andrei Bobrov
Andrei Bobrov el 5 de Mayo de 2017
Editada: Andrei Bobrov el 5 de Mayo de 2017
t = (1:16:145) + (0:5)';
experiment = zeros(72,10,4);
for ii = 1:4
rep = xlsread(sprintf('%d.xlsx',ii),'B9:M158');
experiment(:,:,ii) = reshape(permute(reshape(rep(t,:)',12,6,[]),[2,1,3]),[],10);
xlswrite( sprintf('experiment_%d.xlsx',ii) , experiment(:,:,ii));
end

Santhana Raj
Santhana Raj el 5 de Mayo de 2017
Consider one set of your code:
col1 = rep1(:,1)';
col2 = rep1(:,2)';
col3 = rep1(:,3)';
col4 = rep1(:,4)';
col5 = rep1(:,5)';
col6 = rep1(:,6)';
col7 = rep1(:,7)';
col8 = rep1(:,8)';
col9 = rep1(:,9)';
col10 = rep1(:,10)';
col11 = rep1(:,11)';
col12 = rep1(:,12)';
rep1 =[col1;col2;col3;col4;col5;col6;col7;col8;col9;col10;col11;col12];
rep1 = rep1';
This can be simplified as :
for i=1:12
col(i,:)=rep1(:,1)';
end
rep=col;
you can check the results and probably take a transpose if required.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by