Array size decreases with every iteration
Ältere Kommentare anzeigen
Hi,
I have an array A of size 500,000 x 1
I want to make multiple new arrays by dropping the 1st row, 2nd row, 3rd row etc.
Basically I want a new array B of size 499,999 x 1 which is equal to A(n+1:end,1)
and then a new array C of size 499998 x 1 which is equal to A(n+2:end,1)
etc...
To do this, I have the follow for loop:
i=50;
New_Array = zeros(500000,i);
for n = 1:i
New_Array(n:end-1,n) = A(n+1:end-n,1);
end
The issue is because I preallocated, the rows that it drops off get filled with '0'. I don't want this because I need to multiply A*New_Array and I don't want the dropped off rows with 0's.
If i remove the preallocation, I get an error "The end operator must be used within an array index expression."
I think it's because the array size keeps getting smaller and smaller with every iteration.
How can I fix this?
Thanks!
2 Kommentare
James Tursa
am 9 Feb. 2021
Bearbeitet: James Tursa
am 9 Feb. 2021
Can you show us MATLAB code or pseudo code that takes your inputs, does all of your calculations, and produces the desired output including all of your downstream multiplies? We might be able to suggest a different way to accomplish the end result that is faster and/or uses less memory.
JD
am 10 Feb. 2021
Akzeptierte Antwort
Weitere Antworten (1)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!