Find Min Value in MATLAB Array: No Built-in Functions

In summary, the conversation discusses the attempt to write a function in MATLAB to find the minimum y-value and output the corresponding x value from an array of numbers. The conversation suggests using a for loop instead of recursion and provides a basic algorithm for finding the minimum value in an array.
  • #1
John31
5
0

Homework Statement


I have an array of numbers in MATLAB which I want to located the minimum y-value but output the corresponding x value. (I can explain better if needed) I cannot use a bulit in function so I have tried to write my own. This is what I got so far.


The Attempt at a Solution



Code:
function [m n] = recursiveMin(A)

size = length(A);
if isempty(A)
    m = [];
    n = [];
elseif size == 1
    m = A(1);
    n = 1;
else
    A(1) = max;
    for
        %this is where I am stuck
        
    end
    
end
 
Physics news on Phys.org
  • #2
Thanks for putting your code in [ code] tags!
John31 said:

Homework Statement


I have an array of numbers in MATLAB which I want to located the minimum y-value but output the corresponding x value. (I can explain better if needed) I cannot use a bulit in function so I have tried to write my own. This is what I got so far.


The Attempt at a Solution



Code:
function [m n] = recursiveMin(A)

size = length(A);
if isempty(A)
    m = [];
    n = [];
elseif size == 1
    m = A(1);
    n = 1;
else
    A(1) = max;
    for
        %this is where I am stuck
        
    end
    
end

Why are you doing this recursively? Once you know the size of the array, you can use a for loop to iterate through it and find the smallest value.

Your function doesn't need to return a different array - just the smallest value it found.

The basic algorithm is to set a variable min to the first value in the array, then loop through the rest of the array elements. In each iteration if the value of A(i) is less than min, reset min to the value. When the loop finishes, min will have been set to the smallest value in the array and you can return that value.
 

1. How do I find the minimum value in a MATLAB array without using built-in functions?

To find the minimum value in a MATLAB array without using built-in functions, you can use a for loop to iterate through the array and compare each element to a variable that stores the current minimum value. If the element is smaller than the current minimum value, it becomes the new minimum value. Once the loop is complete, the variable will contain the minimum value in the array.

2. Can I use logical indexing to find the minimum value in a MATLAB array without built-in functions?

Yes, you can use logical indexing to find the minimum value in a MATLAB array without built-in functions. First, create a logical array that identifies the elements in the original array that are smaller than or equal to the current minimum value. Then, use the logical array to index the original array and update the current minimum value. Repeat this process until the logical array contains only false values, indicating that the current minimum value is the overall minimum value in the array.

3. Is there a more efficient way to find the minimum value in a MATLAB array without built-in functions?

Yes, there are several more efficient ways to find the minimum value in a MATLAB array without built-in functions. One method is to use the min function with the 'omitnan' option to ignore any NaN values in the array. Another method is to use the sort function to sort the array in ascending order and then select the first element, which will be the minimum value. Additionally, you can use the find function to locate the index of the minimum value in the array and then access the value using that index.

4. Can I find the minimum value in a specific dimension of a multidimensional MATLAB array without built-in functions?

Yes, you can find the minimum value in a specific dimension of a multidimensional MATLAB array without using built-in functions. To do this, you can use the min function along with the 'omitnan' option and specify the dimension along which you want to find the minimum value.

5. How can I check if my method of finding the minimum value in a MATLAB array without built-in functions is accurate?

To check the accuracy of your method for finding the minimum value in a MATLAB array without built-in functions, you can compare the result to the output of a built-in function like min or minElement. You can also manually check the array to ensure that the minimum value you have found is indeed the minimum value in the array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
809
  • Engineering and Comp Sci Homework Help
Replies
1
Views
940
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top