- pasting it into a script and running the script
- pasting in the command window and pressing enter.
why i my code cant run in command window even though there is no mistake on the script editor that being uploaded
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
% This program tells the user for two 2x3 matrices
% Find the summition for both matrix, compute the total sum and average too
% Input Matricesdisp('Enter the values for the first 2x3 matrix A:')
A = input('Matrix A [8 12 6; 3 14 7]:');
disp(A)
disp('Enter the values for the first 2x3 matrix B:')
B = input('Matrix B [2 3 5; 7 12 4]:');
% Summation for both matrices
C=A+B;
% Show the rwsult of matrix C
disp('The result of matric C=A+B is:')
disp(C)
% Compute sum and average
sumC = sum(C, 'all');
avgC = mean(C, 'all');
% Display all results with the given formatted output using fprintf
fprintf('\nTotal sum of all elements in matrix C: %.2f\n', sumC);
fprintf('Average of all elements in matrix C: %.2f\n', avgC);
2 Commenti
Stephen23
il 19 Apr 2025
Modificato: Stephen23
il 19 Apr 2025
"why i my code cant run in command window even though there is no mistake on the script editor that being uploaded"
Your code runs for me without any error. I tried both:
I provided scalar values for A and B. Please describe the steps you are taking in more detail, so we can understand what you are attempting and where the problem might be (if any).
dpb
il 19 Apr 2025
%A = input('Matrix A [8 12 6; 3 14 7]:');
A = [8 12 6; 3 14 7];
disp(A)
%disp('Enter the values for the first 2x3 matrix B:')
%B = input('Matrix B [2 3 5; 7 12 4]:');
B = [2 3 5; 7 12 4];
disp(B)
% Summation for both matrices
C=A+B;
% Show the rwsult of matrix C
disp('The result of matric C=A+B is:')
disp(C)
% Compute sum and average
sumC = sum(C, 'all');
avgC = mean(C, 'all');
% Display all results with the given formatted output using fprintf
fprintf('\nTotal sum of all elements in matrix C: %.2f\n', sumC);
fprintf('Average of all elements in matrix C: %.2f\n', avgC);
As @Stephen23 notes, the code as posted runs if A,B are matrices of the same size. While the prompt asks specifically for 2x3, the code is valid for any size as long as the two are the same as can be seen by
A = A(:,1:2);
disp(A)
B = B(:,1:2);
disp(B)
C=A+B;
disp('The result of matric C=A+B is:')
disp(C)
sumC = sum(C, 'all');
avgC = mean(C, 'all');
fprintf('\nTotal sum of all elements in matrix C: %.2f\n', sumC);
fprintf('Average of all elements in matrix C: %.2f\n', avgC);
wherein the second are 2x2 instead by simply removing the last column of the initial A,B.
You didn't show what error you got nor the input used to try to run the code, but clearly either the code got corrupted by a spurious character or something when pasting or the input data didn't match up with the expectation/need for both arrays being the same size.
Risposte (1)
dpb
il 19 Apr 2025
As @Stephen23 noted earlier, the code as posted here works; this Answer provides one level of checking that will let the user know if the assumption on array sizes is not met...
% This program asks the user for two 2x3 matrices
% Find the summition for both matrix, compute the total sum and average too
% Input Matricesdisp('Enter the values for the first 2x3 matrix A:')
A = input('Matrix A [8 12 6; 3 14 7]:');
disp(A)
disp('Enter the values for the second 2x3 matrix B:')
B = input('Matrix B [2 3 5; 7 12 4]:');
assert(all(size(A)==size(B)),'A and B are not the same size. Aborting.')
...
rest of code follows. The error message could be amplified to include the actual sizes, etc., but this will at least let know if a mistake was made at that point on input instead of dying when gets to the addition step.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!