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 Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by