Given : Your function header should look like the following:
function [Z,P]=myfun(A)
Find : Create a function myfun that accepts a matrix A as an input. The function should return 2 values...
  • Z - is the total number of elements in A that contain zeros
  • P - is the product of all values that are greater than 2
Issue: I created my function, and it makes sense to me, but I am still getting errors...
My Solution :
function [Z,P]=myfun(A)
Z=sum(A(A==0))
P=prod(A(A>2))
end
% Code used to call funtion:
% A=randi([-5 5],40);
% [Nzero,prod2]=myfun(A)

댓글 수: 7

You should run your code for us so that we can see the errors you are talking about. When I run the code as below, there are no error messages.
% Code used to call funtion:
A=randi([-5 5],40);
[Nzero,prod2]=myfun(A)
Nzero = 0
prod2 = 3.6802e+231
function [Z,P]=myfun(A)
Z=sum(A(A==0));
P=prod(A(A>2));
end
Stephen23
Stephen23 2024년 3월 19일
편집: Stephen23 2024년 3월 19일
"but I am still getting errors..."
No errors (but note the number of zero values in A):
A = randi([-5 5],40)
A = 40×40
-3 -2 5 4 -3 0 -5 -1 3 2 5 -4 -4 -3 3 1 0 -1 -4 0 2 2 -3 -5 2 1 5 -5 4 -4 -1 3 5 -1 1 -3 -2 1 -5 5 -1 -1 2 -1 0 2 -2 -1 -5 -4 3 -4 -3 3 -4 5 -4 1 1 2 1 -5 -1 5 0 -5 -5 3 5 5 2 -1 -2 -1 -5 -2 0 -1 -1 2 5 -3 5 -4 -5 -2 0 -3 0 -1 -1 -4 -5 2 5 0 5 -2 1 0 -3 0 4 5 5 2 -5 3 -3 4 -1 4 -4 0 4 0 2 -5 2 0 2 0 -5 -3 2 -3 4 0 -5 4 -4 -1 -4 2 1 -5 -1 -3 -4 -4 2 1 4 2 5 -5 4 -3 2 4 3 -4 -2 4 -3 3 3 1 1 2 -5 -4 -5 -5 -2 4 5 -1 -5 1 -2 3 2 4 1 2 -1 -2 -2 -2 1 4 5 -5 4 -5 -3 2 -2 -2 5 0 4 4 2 -5 -1 2 5 -5 4 -4 4 -5 -2 3 -5 5 5 0 1 -1 1 3 -1 1 -1 -5 -3 3 0 -2 -1 -3 -4 -5 3 -5 5 -5 -4 4 5 -4 4 -2 -2 -4 3 3 -2 0 -5 5 3 3 3 3 -3 -4 2 -3 -1 1 0 -3 -4 2 2 0 -3 1 2 3 2 -2 -5 2 -1 -2 1 3 0 -4 -5 2 1 -1 -2 -2 4 -5 1 1 1 -3 5 3 0 0 -1 -2 3 -1 5 0 1 -5 -4 -3
[Nzero,prod2] = myfun(A)
Nzero = 0
prod2 = 2.3606e+273
function [Z,P]=myfun(A)
Z=sum(A(A==0));
P=prod(A(A>2));
end
Did you define the function in a script AND have some code after the function definition? As that error message would clearly tell you, in a script any local function definitions must come after all other code:
This was explained in your earlier question:
Spaceman
Spaceman 2024년 3월 20일
편집: Spaceman 2024년 3월 20일
Did you define the function in a script AND have some code after the function definition?
A: No, this function is local, I think. The errors I was referring to were in my code, I should be getting a value for Z that is the sum of all the zeros in the matrix A. But every time I run it I get 0. So this leads me to believe I am not properly coding what I want the outcome to be. My value for P may be correct, but once again I am unsure if I am writing it properly to do what the prompt in my question requires.
My logic for Z is I want the sum of the matrix A, where the values of A are equal to 0.
Stephen23
Stephen23 2024년 3월 20일
"My logic for Z is I want the sum of the matrix A, where the values of A are equal to 0."
And that is what your code does.
But that is not what the assignment requested: it asks that you count elements, not sum their values.
Spaceman
Spaceman 2024년 3월 21일
it asks that you count elements, not sum their values.
Would I need to index here to do so? That is a weak point of mine, among many others.
Steven Lord
Steven Lord 2024년 3월 21일
Recognizing what information a question is asking you is sometime difficult; schools often try to help students develop that skill via word problems starting at an early age, but it's not always easy.
Determining how to calculate that information can be just as tricky, though hopefully not as bad in MATLAB if you have access to the documentation. I tried searching for a phrase from your question in Help Center to see how helpful they'd be.
"number of elements" mostly found functions in the External Language Interfaces which would be good if you were writing code in a different language and calling it from MATLAB, but not quite what you're looking for.
Then I filtered the "number of elements" search to list just Functions (since I'm looking for a function to call to answer the question) and the first three hits look promising: numel, groupcounts, and nnz. numel and nnz are directly applicable to the question; groupcounts would require you to define "everything that's 0" as one group and "everything that's not 0" as another.
Genius. Thanks to ALL of your expert advice, I think I got it in the right direction.
function [Z,P]=myfun(A)
Z=length(A(A==0));
P=prod(A(A>2));
end
% Code used to call funtion:
A=randi([-5 5],40);
[Nzero,prod2]=myfun(A)

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

 채택된 답변

Spaceman
Spaceman 2024년 3월 23일

0 개 추천

function [Z,P]=myfun(A)
Z=length(A(A==0));
P=prod(A(A>2));
end
% Code used to call funtion:
A=randi([-5 5],40);
[Nzero,prod2]=myfun(A)

추가 답변 (0개)

카테고리

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

제품

릴리스

R2023b

태그

질문:

2024년 3월 19일

답변:

2024년 3월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by