Array size decreases with every iteration

조회 수: 2 (최근 30일)
JD
JD 2021년 2월 9일
댓글: Walter Roberson 2021년 2월 10일
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
James Tursa
James Tursa 2021년 2월 9일
편집: James Tursa 2021년 2월 9일
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 2021년 2월 10일
Hi James,
I posted my code and pseudo code for what I'm trying to accomplish below.
Thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 9일
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
JD
JD 2021년 2월 10일
I was able to figure out how to do it!! Thanks for all the help!
Walter Roberson
Walter Roberson 2021년 2월 10일
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)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2021년 2월 9일
You need to read about circshift.
  댓글 수: 1
JD
JD 2021년 2월 9일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by