Problem when Indexing large integer arrays
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (2)
Steven Lord
am 19 Dez. 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
am 19 Dez. 2020
0 Stimmen
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!