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.

回答(1 个)

SANKALP DEV
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.

类别

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

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by