Rearranging of matrix in order to avoid loop

In the following code, my goal is to avoid the for-loop (i=1:m). I have already tried several things, but all of them failed.
m = 3;
n = 10;
ii = [1:n;3:n+2;2:n+1]; % index vector with size (m,n)
jj = [3:n+2;2:n+1;1:n]; % index vector with size (m,n)
load E.mat %external file see attachment
res = zeros(n,n,m);
for i=1:m;
res(:,:,i) = E(ii(i,:),jj(i,:),1,1);
end
How can I avoid the loop? In the attachment you can find the reference output matrix res.mat which contains the matrix res generated by this code. A code without the loop should reproduce the result saved in res.mat.

7 comentarios

Stephen23
Stephen23 el 24 de Jul. de 2017
" I tried to avoid the for-loop (i=1:m)"
What for loop? Your code does not contain the word for anywhere.
Thomas G
Thomas G el 24 de Jul. de 2017
You are right. I edited my question. I think now it should be clear.
Stephen23
Stephen23 el 24 de Jul. de 2017
Editada: Stephen23 el 24 de Jul. de 2017
@Thomas G: I keep editing your question and removing the
if true
% code
end
and you keep adding it back in. IT IS NOT REQUIRED TO SHOW YOUR CODE. All you need is two leading space characters. You do not have to believe me, you can read the markup description yourself:
The text that appears is just a placeholder for you to replace with your own code: it shows where the code should be and how it should look (two leading space, remember), but that text is not required in order to show code on this forum.
Note that the simplest way to get code is to paste it, select it, and then click the {} Code button: this will create correctly formatted code (with two leading space characters). So simple.
"How can I avoid the loop?"
Your example fills the array with random numbers, so the simplest way to avoid the loop is to call rand:
res = rand(n,n,m);
Oh sorry about the code formating issue. I couldn't find the link, you just gave me, which clarifies the code formatting. Thank you.
My example above is just a little toy problem, which is part of a bigger matlab code. In the real problem the matrix E is filled with some precalculated specific values. So I can't use
res = rand(n,n,m)
because I need to fill res with some specific precalculated values of E. Hope that clarifies my problem? I am still looking for a solution without the loop.
Stephen23
Stephen23 el 24 de Jul. de 2017
Editada: Stephen23 el 24 de Jul. de 2017
@Thomas G: I assumed that you are not using random numbers in your calculations, which is why I did not put that as an answer.
However, your question, as posed, is best answered by a simple rand call. Given that you use random numbers to fill that matrix it is impossible to write and test code and know if the matrix has been filled correctly: one random matrix is just as good as another for solving the question that you have asked. Put another way, your question is impossible to falsify: there is no way that we could write code that is wrong, as long as it generates some random numbers. We need to be able to check if code output is wrong.
What would be actually useful is to provide small input and output examples: matrices that actually show what you want to achieve, and that can be used to test code on. You can upload data files by clicking the paperclip button.
Thomas G
Thomas G el 25 de Jul. de 2017
Ok thank you for your hint. I modified the question and added an input and an output file for my loop example. Hope that makes it now precise.

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 25 de Jul. de 2017
Editada: Stephen23 el 25 de Jul. de 2017
>> II = repmat(permute(ii,[2,3,1]),1,10,1);
>> JJ = repmat(permute(jj,[3,2,1]),10,1,1);
>> rex = E(sub2ind(size(E),II,JJ));
>> isequal(rex,res)
ans = 1

5 comentarios

Thomas G
Thomas G el 25 de Jul. de 2017
Wow, thanks a lot. Hopefully I am able to find such a nice solution next time on my own.
Stephen23
Stephen23 el 25 de Jul. de 2017
Editada: Stephen23 el 25 de Jul. de 2017
@Thomas G: to be honest I am not sure that this is useful. It is more complex than the loop you already use, and requires more memory for storing the intermediate variables. What is your motivation for removing your nicely preallocated for loop?
Thomas G
Thomas G el 25 de Jul. de 2017
I thought when I remove all loops, the code is faster, because Matlab performs well on vectors and matrices. Furthermore I heard that Matlab then parallelizes by itself. Is that true? In my real problem the number m can be very high and n is small.
Stephen23
Stephen23 el 25 de Jul. de 2017
@Thomas G: the only way to know for sure is to test it. Write both versions in functions and time them using timeit. You might like to read these as well:
Jan
Jan el 25 de Jul. de 2017
@Thomas G: As a rule of thumb vectorized code is faster than loops, if no huge intermediate index or data arrays are created. When the data process in a loop match into the processor cache, this is a big advantage, and the best vectorized method is lame, if virtual memory must be used, because the physical RAM is exhausted.
Matlab does not parallelize magically. But some commands are multi-threaded, e.g. sum, filter, min, etc. Above a certain size of inputs the work is distributed to the available cores. You can check this roughly in the task manager.

Iniciar sesión para comentar.

Más respuestas (1)

Saeed Bello
Saeed Bello el 25 de Jul. de 2017

0 votos

You can try to look up Matrix indexing and Vectorization in MATLAB. Hope this help.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Jul. de 2017

Comentada:

Jan
el 25 de Jul. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by