Extract values from a matrix using a for loop

2 ビュー (過去 30 日間)
David
David 2018 年 12 月 15 日
コメント済み: dpb 2021 年 1 月 12 日
I am trying to get a 3x20 matrix populated with the eigenvalues from the eig function. The first values are populated in the matrix, that is, column one has three rows of numbers but the other 19 columns are not populating.
The error reads
Index in position 1 exceeds array bounds (must not exceed 3).
my code:
A = [3x60 double]
Eig=zeros(3,20)
for i=1:20
A1=A(i:i+2,i:i+2)
Eig(:,i)=eig(A1)
end
Any help would be greatly appreciated. Thanks.
  2 件のコメント
noor alkhudher
noor alkhudher 2021 年 1 月 12 日
how to do function in for loop for matrix
dpb
dpb 2021 年 1 月 12 日
A = [3x60 double]
...
for i=1:20
A1=A(i:i+2,i:i+2)
What is value of i:i+2 when i >=1?
Don't know what the intent here is, but the reason for the error is clear; you cannot address A(2:4,2:4) when size(A)==>3,60.

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2018 年 12 月 15 日
for i=1:20
A1=A(i:i+2,i:i+2);
...
The first indices into A are the same as the second expression.
i i:i+2 A subscripts
1 1:3 A(1:3,1:3)
2 2:4 A(2:4,2:4)
3 3:5 A(3:5,3:5)
...
Probably what you intended to write was
for i=1:20
A1=A(:,i:i+2);
...
to pick up all three rows for the sequential series of columns...
  2 件のコメント
David
David 2018 年 12 月 15 日
Thanks. This works well.
A1=A(:,i:i+2);
However, I didn’t explain it well. I would like to analyze the first 3x3 group and then the second 3x3 group which starts at the fourth column and so on. Currently, the code you provided is using the first 3x3 group and then going to the second column and using that 3x3 group.
dpb
dpb 2018 年 12 月 16 日
Excuse me? That is the code logic YOU provided, not me! :)
What would the algebraic expression be that would increment the index by 3? Or, alternatively, use the optional increment value to iterate by a non-default value.
Alternatively yet, if you have the Image Processing Toolbox, it contains the function blockproc which applies a function to a defined area of an array and puts the result into an output array...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by