progrem witch sort matrix from user

hey guys
i have a big problem here
i need to write a progrem witch get matrix from user non stop until he writes "0" (not as part of the matrix , only the num "0"
after i get the matrix's i need to sort them by there sum in acending order (the first mat the user wrote will be the one with the smallest sum )
for somereason nothing worked ive been working on it for 6 hours in diffirnent ways and manage to do harder thing
please help me
thx

4 Comments

Please post the code you are currently using and explain the specific problems you are having. Then we can help fix up your code.
first im trying to get the matrix from user
im trying to use a cell array but for some reason its wont work
i=1
while mat{i} ==0
mat{i} = input('Please input matrix ')
end
i get the error
Undefined variable mat.
Error in untitled3 (line 3)
while mat{i} ==0
You're trying to test it it is zero before it is created. You need to create mat{i} first.
ok so i kept going with it , its worked a couple of times , and then suddnly stoped affter 2 matrix input
im trying to sum up all the matrix after input them
then i will sort it
%% Q3
i=1;
mat{1} = input('Please input matrix ');
s(1)=sum(mat{1},'all');
while mat{i} ~= [0];
mat{i+1} = input('Please input matrix ');
s(i+1)=sum(mat{i},'all');
i=i+1;
end

Sign in to comment.

 Accepted Answer

k = 1; % Avoid "i" to reduce confusions with the imaginary unit
ready = false;
mat = {};
while ~ready
mat{k} = input('Please input matrix ');
ready = isequal(mat{k}, 0); % Check if input is 0
k = k + 1;
end
An alternative ís an infinite loop and a break command:
k = 0;
mat = {};
while true % Infinite loop
data = input('Please input matrix ');
if isequal(data, 0)
break; % Jump behind the loop
end
k = k + 1;
mat{k} = data;
end

2 Comments

ok so i kept going with it , its worked a couple of times , and then suddnly stoped affter 2 matrix input
im trying to sum up all the matrix after input them
then i will sort it
%% Q3
i=1;
mat{1} = input('Please input matrix ');
s(1)=sum(mat{1},'all');
while mat{i} ~= [0];
mat{i+1} = input('Please input matrix ');
s(i+1)=sum(mat{i},'all');
i=i+1;
end
Jan
Jan on 10 Nov 2021
Edited: Jan on 10 Nov 2021
I've posted two examples containing isequal(mat{i}, 0). It is a good idea to consider such suggestions. The comparison mat{i} ~= [0] does something else. while and if comnmands require a scalar condition. If you compare a matrix with a scalar, the comparison is done elementwise:
x = rand(2,3);
x == 0
This is a 2x3 matrix. If this is used as condition, Matlab inserts an all() automatically:
while all(mat{i} ~= 0, 'all')
This stops, if any element of mat{i} is zero, while isequal(mat{i}, 0) tests, if the matrix is a scalar 0.
By the way: [] is Matlab's operator for concatenation. [0] concatenates 0 with nothing, so the square brackets are a waste of time only.
The trailing semicolon ; suppresses the output. A while command does not have an output, so it is nicer to omit the semicolon here.
This line:
mat{i+1} = input('Please input matrix ');
appears twice in your code. It is working, but such "code duplications" are a source of bugs, when the code is growing: it happens too frequently, that one of the lines is modified, but the other is not. Therefore it is a good programming practice to avoid duplicate code. See the examples in my answer.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 8 Nov 2021

Edited:

Jan
on 10 Nov 2021

Community Treasure Hunt

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

Start Hunting!