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

James Tursa
James Tursa 2021년 11월 8일
Please post the code you are currently using and explain the specific problems you are having. Then we can help fix up your code.
shay shemesh
shay shemesh 2021년 11월 9일
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
Rik
Rik 2021년 11월 9일
You're trying to test it it is zero before it is created. You need to create mat{i} first.
shay shemesh
shay shemesh 2021년 11월 9일
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 2021년 11월 9일

0 개 추천

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

shay shemesh
shay shemesh 2021년 11월 9일
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 2021년 11월 10일
편집: Jan 2021년 11월 10일
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.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2021년 11월 8일

편집:

Jan
2021년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by