I don't know how to fix my 3D variable y for Intermediate Results for Gauss-Jordan
1 回表示 (過去 30 日間)
古いコメントを表示
function intermediates = rref337i(A)
% Computes the reduced row echelon (A) form of the input matrix (A)
% including a list (pivot_cols) and count (pivot_count) of the pivot columns.
% This code has a pedagogical purpose and ignores certain considerations important
% for practical implmentations including roundoff error and computational efficiency.
% Last updated 9/10/2022
[m n] = size(A); % initialization of the number of rows and columns
pivot_cols = zeros(1,0); % empty initialization of pivot column list
pivot_count = 0; % initialization of the count of pivots
icount = 1;
intermediates(:,:,icount) = A;
for pcc = 1:n
for test_row = pivot_count+1:m
if A(test_row,pcc) ~=0
pivot_count = pivot_count + 1;
pivot_cols(pivot_count) = pcc;
if (test_row~=pivot_count)
A = SWAP(A, test_row, pivot_count);
end
icount = icount + 1;
intermediates(:,:,icount) = A;
for tr = pivot_count+1:m
if (abs(A(tr,pcc))>eps)
A = SUB(A,pivot_count,A(tr,pcc)/A(pivot_count,pcc), tr);
icount = icount + 1;
intermediates(:,:,icount) = A;
end
end
break
end
end
end
for r = pivot_count:-1:1
if A(r,pivot_cols(r)) ~= 1.0
A = SCALE(A,r,1.0/A(r,pivot_cols(r)));
icount = icount + 1;
intermediates(:,:,icount) = A;
end
for tr = 1:r-1
A = SUB(A,r,A(tr,pivot_cols(r)),tr);
icount = icount + 1;
intermediates(:,:,icount) = A;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subfunctions performing row operations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function A = SUB(A,p,multiplier,q)
% Row q is replace by row q minus multiplier times Row p
A(q,:) = A(q,:) - multiplier*A(p,:);
end
function A = SWAP(A,p,q)
% Exchanges row p and row q of the matrix A
temp_row = A(p,:);
A(p,:) = A(q,:);
A(q,:) = temp_row;
end
function A = SCALE(A,p,multiplier)
% Multiplies row p by multiplier
A(p,:) = multiplier*A(p,:);
end
%command code
rref337i([1 2 3; 4 5 6; 7 8 9])
%%When I tested my code, it gives this kind of error, and I don't know
%%where I did wrong. Please help! Thank you.
0 件のコメント
回答 (1 件)
SANKALP DEV
2023 年 9 月 8 日
編集済み: SANKALP DEV
2023 年 9 月 8 日
Hey Seungryul,
I understand that you are trying to compute reduced row echelon form (RREF) of an input 3d matrix, but is getting an error saying, “Variable y must be of size [3 3 9], It is currently of size [3 3 8]”
The error message indicates that the size of the variable ‘intermediates’ is incorrect. It is expected to have a size of ‘[3 3 9]’, but it currently has a size of ‘[3 3 8].’
The issue arises because the ‘intermediates’ variable is initialized with an empty matrix and then assigned values within the loop, but the loop does not count the initial empty matrix when incrementing ‘icount.’
To resolve this issue, you can initialize ‘intermediates’ with the correct size with ‘zeros’ or ‘ones’ and then assign the value to in within the loop.
Consider the following code:
function intermediates = rref337i(A)
[m, n] = size(A);
pivot_cols = zeros(1, 0);
pivot_count = 0;
icount = 1;
intermediates = zeros(m, n, m + 1); % Initialize intermediates with the correct size
intermediates(:, :, icount) = A;
% rest would be same as you did
Hope this helps.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!