recursive function that takes as input an array and prints the output of array in matlab

Here's a recursive function in MATLAB that takes an array as input and prints its contents:

main.m
function printArray(arr)
% Base case: check if array is empty
if isempty(arr)
    return
end

% Recursive case: print first element and recurse on the rest of the array
fprintf('%d ', arr(1))
printArray(arr(2:end))
end
219 chars
11 lines

To use this function, simply call printArray(arr) where arr is your array. The function will recursively print out each element of the array separated by a space.

Note: Depending on the size of the array, using recursion to print out all its contents may not be the most efficient solution. Consider using a loop instead for larger arrays.

gistlibby LogSnag