Help with writing a function
Mostra commenti meno recenti
I gotta write a function that will output a matrix A obtained by modifying an input matrix B in the following way:
- If an entry in B is greater than or equal to zero then multiply it by 5
- If an entry in B is less than zero then add 3 to it.
so far I have tried:
function [x] = create_matrix(a)
if (a>=0)
a*5;
else
a+3;
end
end
but its only adding 3 to all my values. Please help.
Risposte (3)
Image Analyst
il 29 Nov 2014
You can do it with logical indexing. You don't need find(), though you can if you want to use linear indexing instead but it's an extra step. Hint:
indexesToChange = a >= 0;
This creates a logical matrix the same size as "a". Now you can change just those indexes of "a" that satisfied the a>5 criteria:
x=a+3; % Initialize and do the <= situation at the same time.
x(indexesToChange) = a(indexesToChange) * 5;
So that's it, 3 lines and you're done.
Ngoc
il 3 Dic 2014
The function header is wrong. In your initial question, you have an input of matrix B and an output of matrix A. The function header is as follows
function [output]=filename(input)
therefore, yours should have A as the output while B in the input. In your original code, you are saying that your output is X, which is not needed in this code. Additionally, once you change the input and output header, you would have to say that
A=B;
to state that the matrix A is equal to the input of matrix B.
With your if else statement, you are basically comparing all elements in the input matrix. To only find the specific elements in A that satisfy each condition you will have to use the find function and replace the existing values with the values you want like this
A_greater = find(A>=0);
A(A_greater)=A(A_greater).*5;
You would still need to establish the second condition but you could follow the format I have started above. I did not have to use if else statement.
1 Commento
Image Analyst
il 3 Dic 2014
You don't need the find(), though you can. Using find() on the logical indexes would give actual numerical indexes. For example if A>=0 was 1 0 0 1 1, then find(A>=0) would be 1 4 5. It's not necessary here. You can simply use the logical index directly like I showed in my answer, and save a function call:
indexesToChange = a >= 0;
x(indexesToChange) = a(indexesToChange) * 5;
You are right about the sloppy terminology. He says he needs A and B but then his code uses different names, a and x.
Geoff Hayes
il 28 Nov 2014
Florin - one thing to be aware of is that you have defined your output parameter as x but at no time do you ever assign anything to it. At the very least, you should initialize x as
x = a;
before you do any checks as to which elements of a are greater than or equal to zero. Once you have done that, then look at your if condition
if (a>=0)
so the code is checking to see if the matrix a is greater than or equal to zero i.e. are all elements of a greater than or equal to zero. This probably isn't true in your example, and so the code immediately goes to the else statement.
What you really want to do is to find those elements of a that satisfy this condition. You can do that with the find function. Since this is homework, look at the link and see how you would use find and your matrix a (or x since this is the one that is returned) to determine which elements of x are greater than or equal to zero.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!