Array size decreases with every iteration
1 view (last 30 days)
Show older comments
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 Comments
James Tursa
on 9 Feb 2021
Edited: James Tursa
on 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.
Accepted Answer
Walter Roberson
on 9 Feb 2021
i=50;
New_Arrays = cell(i, 1);
for n = 1:i
New_Arrays{i} = A(n+1:end,1);
end
However, this cannot be multiplied by A. Each of your entries is a different size, so they cannot be stored in the same numeric array as each other.
Furthermore, A * New_Array is a matrix multiplication (inner product) between a 50000 x 1 array and things that are individually column vectors. The * operator requires that the second dimension of the left side (A here) be the same as the first dimension of the right side (New_Array here). The second dimension of A is 1, but the first dimension of the right hand side is not 1.
You could ask to calculate A * New_Array.' which would be 50000 x 1 * 1 x 49999 the first time. That would give you a 50000 x 49999 result of dubious value.
My guess at what you want is something like
tril(A(1:50) * A(1:50).')
5 Comments
Walter Roberson
on 10 Feb 2021
A*C would be 6 x 1 column vector * 4 x 1 column vector. That does not work. You could do A * C' to get 6 x 1 * 1 x 4 giving a 6 x 4 output.
I notice from your desired output that you are not using the * operator: you are doing element-by-element multiplication, so like A(1:N) .* A(k+1:k+N)
More Answers (1)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!