Help to make code into a for loop
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I would like to know how to go about making the following code into a for loop:
A1 = [0 1 0; 0 0 0; 0 0 1]
%fprintf('%f\n', A1);
rankA1 = rank(A1);
fprintf('Rank of A1 is %5.0d\n', rankA1);
[r,c] = size(A1);
nullityA1 = c - rankA1;
fprintf('Nullity of A1 is %5.0d\n', nullityA1);
A2 = [4 1 -1; 3 2 0; 1 1 0]
rankA2 = rank(A2);
fprintf('Rank of A2 is %5.0d\n', rankA2);
[r,c] = size(A2);
nullityA2 = c - rankA2;
fprintf('Nullity of A1 is %5.1d\n', nullityA2);
A3 = [1 2 3 4; 0 -1 -1 2; 0 0 0 1]
rankA3 = rank(A3);
fprintf('Rank of A3 is %5.0d\n', rankA3);
[r,c] = size(A3);
nullityA3 = c - rankA3;
fprintf('Nullity of A3 is %5.0d\n', nullityA3);
The code after the matrices A1, A2 and A3 is basically the same except for the variables. Thanks for your help.
0 件のコメント
採用された回答
Geoff Hayes
2016 年 2 月 4 日
編集済み: Geoff Hayes
2016 年 2 月 4 日
monkey_matlab - try creating a cell array of the three matrices A1, A2 and A3. (A cell array is used since these three matrices are of different dimensions.)
matrixData = cell(3,1);
matrixData{1} = A1;
matrixData{2} = A2;
matrixData{3} = A3;
for k=1:length(matrixData)
mtxRank = rank(matrixData{k});
fprintf('Rank of A%d is %d\n', k, mtxRank);
[r,c] = size(matrixData{k});
mtxNulity = c - mtxRank;
fprintf('Nullity of A%d is %d\n', k, mtxNulity);
end
In the above, we iterate over each element in the cell array and consider the rank and nullity for each.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!