Reverse-Engineering Find Function
Show older comments
Consider the MATLAB relational operations: k = (a <= b); m = find(a <= b); where a is a real-valued row vector and b is either a real-valued row vector of the same length as a, or, a scalar.
The objective of this problem is to reverse-engineer MATLAB’s built in function find.
I have to write a MATLAB function, [m,k] = my_find(a,b); that implements the above relational operations using for-loops and if-statements only and does not use find.
The outputs k,m must be the same as those above, and k must be of type logical.
So far, I have:
function [m,k]=my_find(a,b)
for i=1:length(a)
if a<=b
k=1
m=a(a<=b)
else
k=0
end
end
However, when I run the script for the values
a=[1 2 0 -3 7]
b=[3 2 4 -1 7]
I get k=1 as a scalar and not as a vector of 1s. The result for m is correct though, m=[1 2 0 -3 7].
How can I get k to show me the results as a vector of values for all of the elements of a, instead of just the first element being repeated?
I have tried this with a different set of values, and k still sticks to the value of the first element.
Accepted Answer
More Answers (0)
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!