How to vectorize matrix assignment process below

I try to vectorized the following code in the following way to speed up the process. But it doesn't work.
I try to use this code to assign some part of the matrix by values as some functions of the index i (a(i), b(i), .etc).
N=100;
A=zeros(2*N,2*N);
for i=2:N-1
A(2*i-1,2*i-1)=a(i);
A(2*i-1,2*i)=b(i);
A(2*i-1,2*i+1)=c(i);
A(2*i,2*i-3)=d(i);
A(2*i,2*i-1)=e(i);
A(2*i,2*i)=f(i);
end
_______________
vectorized code:
N=100;
A=zeros(2*N,2*N);
i=2:N-1;
A(2*i-1,2*i-1)=a(i);
A(2*i-1,2*i)=b(i);
A(2*i-1,2*i+1)=c(i);
A(2*i,2*i-3)=d(i);
A(2*i,2*i-1)=e(i);
A(2*i,2*i)=f(i);
But matlab says the assignment can be carried out because the dimensions doesn't match. So is there any proper way to do this?

2 件のコメント

Stephan
Stephan 2020 年 11 月 25 日
All values from a(i) are overrided by e(i). Same for b(i) and f(i).
jyp
jyp 2020 年 11 月 25 日
Sorry for the typos. I've corrected them.

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

 採用された回答

KSSV
KSSV 2020 年 11 月 25 日

1 投票

Read about sub2ind. Convert your sub indices into global indices and use.
idx = sub2ind(size(A),2*i-1,2*i-1) ;
A(idx) = a(i) ;
You try to follow something like above.

1 件のコメント

jyp
jyp 2020 年 11 月 25 日
It works fine. Thanks.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2019a

質問済み:

jyp
2020 年 11 月 25 日

コメント済み:

jyp
2020 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by