Reverse-Engineering Find Function
显示 更早的评论
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!