Help to make code into a for loop

9 Ansichten (letzte 30 Tage)
monkey_matlab
monkey_matlab am 4 Feb. 2016
Bearbeitet: Geoff Hayes am 4 Feb. 2016
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.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 4 Feb. 2016
Bearbeitet: Geoff Hayes am 4 Feb. 2016
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.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Mobile finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by