필터 지우기
필터 지우기

Using nested loop to enter individual values in a matrix

조회 수: 1 (최근 30일)
Wilbur
Wilbur 2023년 3월 7일
답변: Voss 2023년 3월 7일
Hi, I am trying to use a nested loop to input values of a m x m matrix individually. However after populating the entire matrix it still asks me for one additional value!
How should I solve this problem?
% Inputs
% [A] = M x M matrix
close all, clear all, clc;
% Ask user for size of matrices where [A] = M x M
m = input('What is the value of M, where matrices [A] = m x m? ');
% Create matrices [A]
A = [];
x = 1; y = 1;
while y < m+1;
while x < m+1;
i = input('Matrix A - input value of row x column y: ');
A(y,x) = i;
x = x+1;
end
x = 1;
y = y+1;
j = input('Matrix A - input value of row y column x: ');
A(y, x) = j;
x = x+1;
end
A

채택된 답변

Voss
Voss 2023년 3월 7일
Basically, you don't need the second input(), after the inner while loop, at all.
close all, clear all, clc;
% Ask user for size of matrices where [A] = M x M
m = input('What is the value of m, where matrix [A] = m x m? ');
% Create matrices [A]
A = zeros(m,m);
y = 1;
while y < m+1
x = 1;
while x < m+1
i = input(sprintf('Matrix A - input value of row %d column %d: ', y, x));
A(y,x) = i;
x = x+1;
end
y = y+1;
end
A

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by