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.

采纳的回答

Geoff Hayes
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 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by