for loop in matlab
I want to write a script that first asks for some inputs:
A = input('Enter a square matrix A: ');
and if it's not a square matrix, it would display an error and then ask again for entering matrix A
and based on A's dimension, the program in the command line asks next for the following inputs: (suppose A's dimension is 3)
Enter 3x1-matrix B:
Enter 1x3-matrix C:
Then, it would ask for the initial values x(0)'s, where they are 3 x(0)'s in our situation:
The initial values:
x1(0):
x2(0):
x3(0):
(Note: the number of x(0)'s depends on A's dimension. We can stores the intial values in the var开发者_Python百科iables x1,x2 and x3)
How can I code this in Matlab?
thanks in advance!
=======================
my try:
while(true)
A = input('Enter square matrix A: ');
sizeA = size(A);
if sizeA(1) == sizeA(2)
break;
end
display('Error: You have to enter square matrix!')
end
while(true)
B = input('Enter ' + sizeA(1) + 'x1-matrix B: ');
sizeB = size(A);
if sizeB(1) == sizeA(1) && sizeB(2) == 1
break;
end
display('Error: You have to enter ' + sizeA(1) + 'x1-matrix!')
end
while(true)
C = input('Enter 1x' + sizeA(1) + '-matrix C: ');
sizeC = size(A);
if sizeC(1) == 1 && sizeC(2) == sizeA(1)
break;
end
display('Error: You have to enter 1x' + sizeA(1) + '-matrix!')
end
display('The initial values: ');
% well, I don't know how to start writing the code for the initial values
========================
Finally, I got what I want
while(true)
A = input('Enter square matrix A: ');
sizeA = size(A);
if sizeA(1) == sizeA(2) && ndims(A) == 2
break;
end
display('Error: You have to enter square matrix!')
end
while(true)
B = input(['Enter ' , num2str(sizeA(1)) , 'x1-matrix B: ']);
sizeB = size(B);
if sizeB(1) == sizeA(1) && sizeB(2) == 1 && ndims(B) == 2
break;
end
display(['Error: You have to enter ' , num2str(sizeA(1)) , 'x1-matrix!'])
end
while(true)
C = input(['Enter 1x' , num2str(sizeA(1)) , '-matrix C: ']);
sizeC = size(C);
if sizeC(1) == 1 && sizeC(2) == sizeA(1) && ndims(C) == 2
break;
end
display(['Error: You have to enter 1x' , num2str(sizeA(1)) , '-matrix!'])
end
display('The initial values: ');
for i=1:sizeA(1)
x(i) = input(['Enter X' , num2str(i) , '(0): ']);
end
You haven't shown us how far you've gotten or what you've tried. Remember that SO is not a place to request for us to do your entire code, but to ask for help in solving specific issues.
However, I don't want to leave you hanging there without help. So, you can try using the following functions to write your script. You can always edit your post to ask a more specific question once you have the basic script working.
- Look at the
input
function for requesting user input - Use the
error
function to throw an error. - Use
size
to get the dimensions of an array. Use two output arguments like[rows cols]=size(A)
to get both the dimensions. - It's not just enough to check if
rows==cols
. You'll also need to confirm that there are only two dimensions usingndims
, as if there are more than two dimensions and you request only two outputs from size, it combines the second and higher dimensions into the second output. As an example, see what you get for[rows,cols]=size(rand(4,2,2))
- You can use
isequal
along with some of the above to verify if the input dimensions for matricesB
andC
are correct. - You can either store your initial values as a cell as suggested by kwatford or as a vector. I can't suggest which one without knowing what you're going to do with it.
Hope these helped get you started.
精彩评论