What would I need to make 'i' allow all matrices?

1 回表示 (過去 30 日間)
Collin Kerr
Collin Kerr 2016 年 3 月 31 日
回答済み: MHN 2016 年 4 月 1 日
clc,clear
prompt = 'Give a matrix of 4x4: ';
x = input(prompt);
for i=1:16
if x(i)<=0
disp('Error one of the numbers put in was either a zero or negative, Fix it and start over.')
if i~=1:16
disp('The matrix is not a 4x4 please start again and fix the error.')
end
end
end
disp(x)
Cause at the moment Im only allowed to do 4x4 or 4x5 or 4x6 i cant do 3x3 or 4x3 etc...
  1 件のコメント
John D'Errico
John D'Errico 2016 年 3 月 31 日
Please don't post the same question again just to make a minor edit to the first one.

サインインしてコメントする。

採用された回答

MHN
MHN 2016 年 3 月 31 日
編集済み: MHN 2016 年 3 月 31 日
prompt = 'Give a matrix of 4x4: ';
x = input(prompt);
[m,n] = size(x);
if m~=4 | n~= 4
disp('The matrix is not a 4x4 please start again and fix the error.')
else
for i=1:16
if x(i)<=0
disp('Error one of the numbers put in was either a zero or negative, Fix it and start over.')
end
end
end
disp(x)

その他の回答 (1 件)

MHN
MHN 2016 年 4 月 1 日
prompt = 'Give a matrix of 4x4: ';
flag = 0;
while flag ==0
x = input(prompt);
[m,n] = size(x);
if m==4 && n==4
flag = 1;
else
disp('The matrix is not a 4x4 please start again and fix the error.')
end
if ~isempty(find(x<=0,1))
disp('Error one of the numbers put in was either a zero or negative, Fix it and start over.')
flag = 0;
end
end

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by