필터 지우기
필터 지우기

Help to make code into a for loop

조회 수: 1 (최근 30일)
monkey_matlab
monkey_matlab 2016년 2월 4일
편집: Geoff Hayes 2016년 2월 4일
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