Problem when Indexing large integer arrays

3 vues (au cours des 30 derniers jours)
Carlos Rueda
Carlos Rueda le 18 Déc 2020
Modifié(e) : Carlos Rueda le 19 Déc 2020
I created following code that is suposed to reverse a vector with huge length, in the order of hundreds of thousands:
function y=reversal(v)
n=25000;
if length(v)<n
y=[v(end):-1:v(1)];
else
y=[v(end:-1:(end-n)), reversal(v(1:end-n-1))];
end
end
The code is supposed to be recursive but supposed to split the input vector, since one million of single recursions will cause a memory error. With the default input, all the integers from 1 to 1 million it worked (v=(1:1e6)). With random integer array inputs the code fails in the size of the output array. I understand how that happens, if I split the input vector, for instance randi(1000,1,700725), the code will fail by the size of the output vector. Moreover, everytime I run the code with the same input vector (in terms of size and content, the size of reversed vector will also change.
I don't understand how the size of the reversed vector changes every time and how the way I split input vectors works for (1:1e6) , (1:999999) but not for the large integer vectors.
If anyone can give me an explanation for that I will be very grateful.

Réponse acceptée

James Tursa
James Tursa le 18 Déc 2020
Modifié(e) : James Tursa le 18 Déc 2020
This
[v(end):-1:v(1)]
creates an indexing array that starts at the value v(end), increments by -1, and ends with v(1). It does not reverse the vector. To reverse the vector you need to have all the indexing inside the parentheses. E.g.,
v(end:-1:1)
E.g., if you had the following
v = [3 5 1 4 2]
then this code
[v(end):-1:v(1)]
would produce this result
2:-1:3
which would be empty.
But this code
v(end:-1:1)
would produce
[2 4 1 5 3]
If you fix your coding you will probably find that you don't need recursion for this.

Plus de réponses (2)

Steven Lord
Steven Lord le 19 Déc 2020
If you call flip, fliplr, or flipud as appropriate you don't need to do the indexing yourself.
But since this is almost certainly homework, I'm guessing your professor forbade you from using those functions.
If you want to try to puzzle this out, think about breaking a smaller vector into smaller chunks.
x = 1:7
n = 3
If I told you to apply this technique to the x and n above can you write out the steps that you should follow with pencil and paper? If so can you then figure out how to convert those manual steps into code?

Carlos Rueda
Carlos Rueda le 19 Déc 2020
Thank you guys for you contribution. I now understand why my code was failing when input is not a multiple of 25,000. Thank you, James.
And thank you, Steven, as you could correctly guess, I am not allowed to use built-in functions and I must use recursion. So my code works now, maybe not the most elegant solution, but I am just getting started since early November.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by