Calculate number in a matrix

조회 수: 2 (최근 30일)
Yu-Wei Tu
Yu-Wei Tu 2021년 5월 20일
댓글: Yu-Wei Tu 2021년 5월 20일
Hi, how can I calculate a matrix by asking a user's input? For example, ask user to input a matrix such as [1 5 3] and change the middle number by adding the numbers next to it and take average, So in this case it would be [1 (1+5+3)/3 3] which will output [1 3 3]. Thank you.
  댓글 수: 2
Jan
Jan 2021년 5월 20일
This sounds like a homework question. Then please post, what you have tried so far and ask a specific question.
Yu-Wei Tu
Yu-Wei Tu 2021년 5월 20일
It's not a homework question, there's no specific number for the matrix, I'm just trying to describe the calculation.

댓글을 달려면 로그인하십시오.

채택된 답변

Atsushi Ueno
Atsushi Ueno 2021년 5월 20일
편집: Atsushi Ueno 2021년 5월 20일
> how can I calculate a matrix by asking a user's input?
input function is the best solution. Request user input - MATLAB input - MathWorks
prompt = 'input a matrix such as [1 5 3]? ';
try
x = input(prompt);
if all(size(x) == [1 3])
x(2) = sum(x) / 3.0;
disp(['calculated result is [' num2str(x) ']']);
else
disp('size of input matrix is not 1 row by 3 columns.');
end
catch
disp('input style is not as a matrix!');
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by