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

  • Thread starter Thread starter John31
  • Start date Start date
  • Tags Tags
    Matlab Value
Click For Summary
SUMMARY

The discussion focuses on finding the minimum value in a MATLAB array without using built-in functions. A user attempts to create a recursive function named recursiveMin but encounters issues with implementation. The recommended approach is to utilize a for loop to iterate through the array, setting a variable min to the first element and updating it whenever a smaller value is found. This method simplifies the process and avoids unnecessary recursion.

PREREQUISITES
  • Understanding of MATLAB programming language
  • Familiarity with array manipulation in MATLAB
  • Basic knowledge of control structures, specifically loops
  • Concept of recursion in programming
NEXT STEPS
  • Implement a for loop in MATLAB to find the minimum value in an array
  • Explore MATLAB's array indexing techniques for efficient data access
  • Learn about recursion and its applications in MATLAB
  • Study error handling in MATLAB functions to manage edge cases
USEFUL FOR

This discussion is beneficial for MATLAB programmers, students tackling algorithmic challenges, and anyone looking to enhance their skills in array manipulation without relying on built-in functions.

John31
Messages
5
Reaction score
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
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.
 

Similar threads

Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K