MATLAB - map 函式



MATLAB 中的 map 函式是一個強大的工具,允許你將特定的操作或函式應用於陣列或元胞陣列中的每個元素,並將結果返回到新的陣列或元胞陣列中。當你想對多個數據點執行相同的操作而無需編寫迴圈時,這尤其有用。

Matlab 沒有內建的 map 函式,但是,你可以使用其他內建函式和結構來實現類似的功能。

對陣列使用 arrayfun

MATLAB 中的 arrayfun 函式允許你將函式應用於陣列的每個元素,類似於對映的概念。

語法

B = arrayfun(func, A)

語法的詳細解釋如下:

B - 這是輸出陣列,它是將函式 func 應用於輸入陣列 A 的每個元素的結果。

func - 這是你想要應用於輸入陣列每個元素的函式。它可以是匿名函式或函式控制代碼。func 應該接受一個輸入引數(A 的當前元素)並返回單個值。

A - 這是你想要應用函式 func 的輸入陣列。

示例 1

假設你有一個數字陣列,並且陣列中的每個數字都必須平方。為此,我們可以使用 arrayfun,如下例所示。

% This is a function to square a number
square_function = @(x) x^2;

% Array of numbers to be squared 
input_array = [1, 2, 3, 4, 5];

% Use arrayfun to apply the square function to each element
output_array = arrayfun(square_function, input_array);

% Display the result
disp(output_array)

在這個例子中,square_function 應用於 input_array 中的每個元素,結果儲存在 output_array 中。結果是 [1, 4, 9, 16, 25]。

arrayfun 簡化了將函式應用於陣列每個元素的過程,並幫助我們避免使用迴圈來執行相同操作。

當你在 matlab 命令視窗中執行相同的操作時,輸出為:

>> % This is a function to square a number
square_function = @(x) x^2;

% Array of numbers to be squared 
input_array = [1, 2, 3, 4, 5];

% Use arrayfun to apply the square function to each element
output_array = arrayfun(square_function, input_array);

% Display the result
disp(output_array)

     1     4     9    16    25

對元胞陣列使用 cellfun

對於元胞陣列,你可以使用 cellfun 函式將函式應用於元胞陣列的每個元素。

語法

B = cellfun(func, C)

B - 這是輸出元胞陣列,它是將函式 func 應用於輸入元胞陣列 C 的每個元素的結果。

func - 這是你想要應用於元胞陣列每個元素的函式。func 可以是匿名函式或函式控制代碼。該函式應該接受一個輸入引數(C 的當前元素)並返回單個值。

C - 這是你想要應用函式 func 的輸入元胞陣列。

示例 1:將函式應用於字串元胞陣列

假設你有一個字串元胞陣列,並且你想要知道元胞陣列中每個字串的長度。為此,你可以使用 cellfun(),如下例所示:

% Create a cell array of strings
input_cell_array = {'apple', 'banana', 'cherry'};

% Define a function to find the length of a string
length_function = @(str) length(str);

% Use cellfun to apply the length function to each element
output_array = cellfun(length_function, input_cell_array);

% Display the result
disp(output_array)

在這個例子中,length_function 應用於 input_cell_array 中的每個字串,結果儲存在 output_array 中。

當你在 matlab 命令視窗中執行上述程式碼時,輸出如下:

>> % Create a cell array of strings
input_cell_array = {'apple', 'banana', 'cherry'};

% Define a function to find the length of a string
length_function = @(str) length(str);

% Use cellfun to apply the length function to each element
output_array = cellfun(length_function, input_cell_array);

% Display the result
disp(output_array)

     5     6     6

示例 2:將函式應用於數字元胞陣列

假設你有一個數值陣列的元胞陣列,並且你想要找到每個數值陣列的總和。為此,讓我們使用 cellfun() 應用於每個元胞陣列,如下所示:

% Create a cell array of numeric arrays
numeric_cells = { [1, 2, 3], [4, 5], [6, 7, 8, 9] };

% Define a function to compute the sum of an array
sum_function = @(arr) sum(arr);

% Use cellfun to apply the sum function to each element
output_array = cellfun(sum_function, numeric_cells);

% Display the result
disp(output_array)

在這個例子中,sum_function 應用於 numeric_cells 元胞陣列中的每個數值陣列,結果儲存在 output_array 中。讓我們透過在 matlab 命令視窗中執行上述程式碼來檢查輸出。

>> % Create a cell array of numeric arrays
numeric_cells = { [1, 2, 3], [4, 5], [6, 7, 8, 9] };

% Define a function to compute the sum of an array
sum_function = @(arr) sum(arr);

% Use cellfun to apply the sum function to each element
output_array = cellfun(sum_function, numeric_cells);

% Display the result
disp(output_array)

     6     9    30
廣告
© . All rights reserved.