I do not want to use assignin,evalin or eval. If you run the code you'll see that at the bottom the matrices for M_A, M_B, M_C and M_D are displayed. Please can you show me how I can have these variables as outputs and their matrices in the workspace? So far only the first output value (M_A)is showing up as an output, as "ans". I'd like it to say M_A, and same for the others (M_B, M_C and M_D). Please help
function [M_A, M_B, M_C, M_D] = DiagDom
A = [ 1 4 0 1 1; -1 -5 9 -20 2 ; 15 1 4 5 1 ; 2 2 -5 0 0; 0 2 -3 1 -9];
B= [ 9 3 3; 12 3 2; 1 3 5];
C= [ 1 13 2; 1 3 9; -12 2 -1];
D= [ 5 -2 4 2; 0 3 -1 -1; 3 3 9 -5;1 1 1 5];
oldA = [ 1 4 0 1 1; -1 -5 9 -20 2 ; 15 1 4 5 1 ; 2 2 -5 0 0; 0 2 -3 1 -9];
oldB= [ 9 3 3; 12 3 2; 1 3 5];
oldC= [ 1 13 2; 1 3 9; -12 2 -1];
oldD= [ 5 -2 4 2; 0 3 -1 -1; 3 3 9 -5;1 1 1 5];
M = {A,B,C,D};
for i=1:numel(M)
count = 0;
while(1) % Perform infinite loop, till you find the diagonally dominant matrix
if itisDiagDom (M{i}) % If this is diagonally dominant, disp and break the loop
disp (['Matrix M is diagonally-dominant']);
celldisp(M) ;
disp([count]);
break;
else
M{i} = M{i}(randperm(size(M{i}, 1)), :);
count= count+1 ;
disp([count]);
if count > 500
if itisDiagDom (M{1})
M_A = M{1}
else
M_A = oldA
end
if itisDiagDom (M{2})
M_B= M{2}
else
M_B= oldB
end
if itisDiagDom (M{3})
M_C= M{3}
else
M_C= oldC
end
if itisDiagDom (M{4})
M_D= M{4}
else
M_D= oldD
end
break ;
end
% Randomly swaps rows
end
end
end
end
function [isdom] = itisDiagDom( A )
isdom = true;
for r = 1:size(A,1)
rowdom = 2 * abs(A(r,r)) > sum(abs(A(r,:)));
isdom = isdom && rowdom;
end
if isdom == 0
disp (['Matrix']);
disp([A]);
disp([' is not diagonally-dominant']);
elseif isdom == 1
disp (['Matrix is diagonally-dominant']);
disp([A]);
end
end

댓글 수: 5

Original question by alroyp10, retrieved from Google Cache:
My function only stores the first output in the workspace :(
I do not want to use assignin,evalin or eval. If you run the code you'll see that at the bottom the matrices for M_A, M_B, M_C and M_D are displayed. Please can you show me how I can have these variables as outputs and their matrices in the workspace? So far only the first output value (M_A)is showing up as an output, as "ans". I'd like it to say M_A, and same for the others (M_B, M_C and M_D). Please help
function [M_A, M_B, M_C, M_D] = DiagDom
A = [ 1 4 0 1 1; -1 -5 9 -20 2 ; 15 1 4 5 1 ; 2 2 -5 0 0; 0 2 -3 1 -9];
B= [ 9 3 3; 12 3 2; 1 3 5];
C= [ 1 13 2; 1 3 9; -12 2 -1];
D= [ 5 -2 4 2; 0 3 -1 -1; 3 3 9 -5;1 1 1 5];
oldA = [ 1 4 0 1 1; -1 -5 9 -20 2 ; 15 1 4 5 1 ; 2 2 -5 0 0; 0 2 -3 1 -9];
oldB= [ 9 3 3; 12 3 2; 1 3 5];
oldC= [ 1 13 2; 1 3 9; -12 2 -1];
oldD= [ 5 -2 4 2; 0 3 -1 -1; 3 3 9 -5;1 1 1 5];
M = {A,B,C,D};
for i=1:numel(M)
count = 0;
while(1) % Perform infinite loop, till you find the diagonally dominant matrix
if itisDiagDom (M{i}) % If this is diagonally dominant, disp and break the loop
disp (['Matrix M is diagonally-dominant']);
celldisp(M) ;
disp([count]);
break;
else
M{i} = M{i}(randperm(size(M{i}, 1)), :);
count= count+1 ;
disp([count]);
if count > 500
if itisDiagDom (M{1})
M_A = M{1}
else
M_A = oldA
end
if itisDiagDom (M{2})
M_B= M{2}
else
M_B= oldB
end
if itisDiagDom (M{3})
M_C= M{3}
else
M_C= oldC
end
if itisDiagDom (M{4})
M_D= M{4}
else
M_D= oldD
end
break ;
end
% Randomly swaps rows
end
end
end
end
function [isdom] = itisDiagDom( A )
isdom = true;
for r = 1:size(A,1)
rowdom = 2 * abs(A(r,r)) > sum(abs(A(r,:)));
isdom = isdom && rowdom;
end
if isdom == 0
disp (['Matrix']);
disp([A]);
disp([' is not diagonally-dominant']);
elseif isdom == 1
disp (['Matrix is diagonally-dominant']);
disp([A]);
end
end
alexp10
alexp10 2020년 5월 4일
Bro...relax. People make mistakes. What do you gain from getting mad?
Rik
Rik 2020년 5월 4일
편집: Rik 2020년 5월 4일
"I'd like to delete this thread. Stephen Cobeldick is purposely posting my question because he's frustrated that I don't understand functions. I made a silly mistake, I'd like to delete the thread please."
Although I could be wrong, I doubt he is frustrated because you don't understand functions. I suspect he is frustrated you removed your question, which removes a lot of the usefulnes of his answer. (I also don't really see an indication of him being mad) If you come across a post in you search for the answer to some question, would you read the answer to a question that states 'wefwef'?
Silly mistakes are fine. Everybody makes them. What is not fine is attempting to delete your question after you have received help. If you don't want people to know you silly mistakes, you shouldn't post anything to the internet at all. At some point you will make a silly mistake.
alexp10
alexp10 2020년 5월 4일
No, he deleted his comment after his answer where he was frustrated (because I was a beginner in matlab). He was frustrated before, that's why I wanted to delete the question. I appreciated his first answer. Not his second one. But he deleted that.
Rena Berman
Rena Berman 2020년 5월 14일
(Answers Dev) Restored edit

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

 채택된 답변

Stephen23
Stephen23 2020년 5월 3일
편집: Stephen23 2020년 5월 3일

0 개 추천

You need to call the function with all of its output arguments, e.g. (pick the names to suit your needs):
[A,B,C,D] = DiagDom();
Very basic MATLAB concepts, e.g. how to call functions with multiple output arguments, are explained in the introductory tutorials:

댓글 수: 4

[A,B,C,D] = DiagDom(
Hi, yes I can't seem to get my head around this. I don't want A,B,C,D to be my outputs, they are the inputs. They get checked if theyre diagonally dominant by swapping rows. The outputs are the diagonally dominant matrices, if they can't be made diagonally dominant then they're stored as the original matrix.
At the location from which you are calling your function, MATLAB has no idea that A, B, C, D, M_A, M_B, M_C, and M_D exist within your function. It only knows that four variables are output by your function, and this line:
[A, B, C, D] = DiagDom();
assigns those four variables to A, B, C, and D, which are entirely different from the A, B, C, and D inside your function. If you'd like to store the four outputs in variables with different names, it is easy to do so:
[M_A, M_B, M_C, M_D] = DiagDom();
or maybe
[out1, out2, out3, out4] = DiagDom();
alexp10
alexp10 2020년 5월 3일
Thankyou Tommy! Turns out that I had to write that line outside the function. Thanks alot!
Tommy
Tommy 2020년 5월 3일
편집: Tommy 2020년 5월 3일
"Reason: Another answer is better"
alroyp10, only one answer was posted in response to the question, and it did sufficiently answer what was asked.
"how do i delete questions?"
See the third paragraph here:
The first paragraph here:
And many of the responses here:

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

태그

질문:

2020년 5월 3일

댓글:

2020년 5월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by