Array size decreases with every iteration

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
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
JD am 10 Feb. 2021
Hi James,
I posted my code and pseudo code for what I'm trying to accomplish below.
Thanks!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Feb. 2021

1 Stimme

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 Kommentare

JD
JD am 9 Feb. 2021
Thanks Walter! That helped with the first part. I’ll try some stuff for the second part. I’m not sure tril is what I want. I want to multiply my original array A with each of the new arrays inside the cell matrix ‘New Array’. I’m trying to figure out how to do this using only the first 50 rows of each of the new arrays inside the cell matrix.
Walter Roberson
Walter Roberson am 9 Feb. 2021
When you "multiply" your 50000 x 1 vector by the first entry in the cell, a 49999 x 1 vector, what output size are you expecting?
JD
JD am 10 Feb. 2021
Bearbeitet: JD am 10 Feb. 2021
Hi Walter,
So below is what I'm trying to do
for k = 1:i
M = A(1:499992).*New_Array{i}(1:499992);
end
Where A is the original array and New Array is the modified cell array of size (i,1)
For example if I have an original array
A = [1; 2; 3; 4; 5; 6]
I first want to make new arrays such as the following
B = [2; 3; 4; 5; 6]
C = [3; 4; 5 ;6]
and then I want
A*B = [2; 6; 12; 20; 30]
and
A*C = [3; 8; 15; 24]
I was able to do the first part which is make the new B and C arrays with the method you suggested above.
And then I was able to do the second part to find A*B and A*C by using the code I just posted above. However, I am not sure how to save "M" for different values of i.
Thanks
JD
JD am 10 Feb. 2021
I was able to figure out how to do it!! Thanks for all the help!
Walter Roberson
Walter Roberson am 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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 9 Feb. 2021

0 Stimmen

You need to read about circshift.

1 Kommentar

JD
JD am 9 Feb. 2021
Are you saying I could write a for loop to shift each new matrix array to get rid of all the rows with 0’s?

Melden Sie sich an, um zu kommentieren.

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!

Translated by