I want to copy the rows of G1 to G2 in inner for loop but error occurs , please help

1 回表示 (過去 30 日間)
Junaid Khan
Junaid Khan 2016 年 11 月 13 日
編集済み: Walter Roberson 2016 年 11 月 14 日
G1 = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1;
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1;
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1;
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1;
0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1;
0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1;
0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1;
0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1;
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1];
m=4;
r=4;
k=0;
n=2^m;
k=15;
for a=r:-1:2
M=factorial(m)/(factorial(m-a)*factorial(a));
G2 = zeros(M,n);
for o=1:1:M
G2(o,:) = G1(k,:);
k=k-1;
end
end

回答 (1 件)

Daniel kiracofe
Daniel kiracofe 2016 年 11 月 13 日
the first time through the loops, k = 15. but size(G1) is 11x16. So the first time you are effectively doing
G2(o,:) = G1(15,:)
which doesn't work. Maybe you meant this instead?
G2(:, o) = G1(:,15)
  2 件のコメント
Junaid Khan
Junaid Khan 2016 年 11 月 13 日
This this the complete code, value of a decrease , M is computed and G2 is generated having rows equal to M, than i want to copy G1 rows portion to G2 in each iteration of for loop, the complete code is given below G1 = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1; 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1; 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1; 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1; 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1; 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1; 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1; 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1; 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1; 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1]; m=4; r=4; k=0; n=2^m; for i=1:r k= k+factorial(m)/(factorial(m-i)*factorial(i));
end
for a=r:-1:2 M=factorial(m)/(factorial(m-a)*factorial(a)); G2 = zeros(M,n); for o=M:1:1 G2(o,:) = G1(k,:); k=k-1; end end
Daniel kiracofe
Daniel kiracofe 2016 年 11 月 13 日
That doesn't change anything about my previous comment. at the conclusion of this section of the code
for i=1:r k= k+factorial(m)/(factorial(m-i)*factorial(i)) end
the variable k has the value 15. And G1 has size 11x16 = 11 rows and 16 columns.. So G1(k,:) is asking to access the 15th row of a matrix that has only 11 rows. You can't copy a row that doesn't exist.

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

カテゴリ

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