- Matlab 教程
- MATLAB - 首頁
- MATLAB - 概述
- MATLAB - 特性
- MATLAB - 環境設定
- MATLAB - 編輯器
- MATLAB - 線上版
- MATLAB - 工作區
- MATLAB - 語法
- MATLAB - 變數
- MATLAB - 命令
- MATLAB - 資料型別
- MATLAB - 運算子
- MATLAB - 日期和時間
- MATLAB - 數字
- MATLAB - 隨機數
- MATLAB - 字串和字元
- MATLAB - 文字格式化
- MATLAB - 時間表
- MATLAB - M 檔案
- MATLAB - 冒號表示法
- MATLAB - 資料匯入
- MATLAB - 資料匯出
- MATLAB - 資料歸一化
- MATLAB - 預定義變數
- MATLAB - 決策
- MATLAB - 決策語句
- MATLAB - if end 語句
- MATLAB - if else 語句
- MATLAB - if…elseif else 語句
- MATLAB - 巢狀 if 語句
- MATLAB - switch 語句
- MATLAB - 巢狀 switch
- MATLAB - 迴圈
- MATLAB - 迴圈
- MATLAB - for 迴圈
- MATLAB - while 迴圈
- MATLAB - 巢狀迴圈
- MATLAB - break 語句
- MATLAB - continue 語句
- MATLAB - end 語句
- MATLAB - 陣列
- MATLAB - 陣列
- MATLAB - 向量
- MATLAB - 轉置運算子
- MATLAB - 陣列索引
- MATLAB - 多維陣列
- MATLAB - 相容陣列
- MATLAB - 分類陣列
- MATLAB - 元胞陣列
- MATLAB - 矩陣
- MATLAB - 稀疏矩陣
- MATLAB - 表格
- MATLAB - 結構體
- MATLAB - 陣列乘法
- MATLAB - 陣列除法
- MATLAB - 陣列函式
- MATLAB - 函式
- MATLAB - 函式
- MATLAB - 函式引數
- MATLAB - 匿名函式
- MATLAB - 巢狀函式
- MATLAB - return 語句
- MATLAB - 無返回值函式
- MATLAB - 區域性函式
- MATLAB - 全域性變數
- MATLAB - 函式控制代碼
- MATLAB - filter 函式
- MATLAB - 階乘
- MATLAB - 私有函式
- MATLAB - 子函式
- MATLAB - 遞迴函式
- MATLAB - 函式優先順序
- MATLAB - map 函式
- MATLAB - mean 函式
- MATLAB - end 函式
- MATLAB - 錯誤處理
- MATLAB - 錯誤處理
- MATLAB - try…catch 語句
- MATLAB - 除錯
- MATLAB - 繪圖
- MATLAB - 繪圖
- MATLAB - 繪製陣列
- MATLAB - 繪製向量
- MATLAB - 條形圖
- MATLAB - 直方圖
- MATLAB - 圖形
- MATLAB - 二維線圖
- MATLAB - 三維圖
- MATLAB - 格式化繪圖
- MATLAB - 對數座標軸圖
- MATLAB - 繪製誤差條
- MATLAB - 繪製三維等值線圖
- MATLAB - 極座標圖
- MATLAB - 散點圖
- MATLAB - 繪製表示式或函式
- MATLAB - 繪製矩形
- MATLAB - 繪製頻譜圖
- MATLAB - 繪製網格曲面
- MATLAB - 繪製正弦波
- MATLAB - 插值
- MATLAB - 插值
- MATLAB - 線性插值
- MATLAB - 二維陣列插值
- MATLAB - 三維陣列插值
- MATLAB - 多項式
- MATLAB - 多項式
- MATLAB - 多項式加法
- MATLAB - 多項式乘法
- MATLAB - 多項式除法
- MATLAB - 多項式的導數
- MATLAB - 變換
- MATLAB - 變換函式
- MATLAB - 拉普拉斯變換
- MATLAB - 拉普拉斯濾波器
- MATLAB - 高斯-拉普拉斯濾波器
- MATLAB - 逆傅立葉變換
- MATLAB - 傅立葉變換
- MATLAB - 快速傅立葉變換
- MATLAB - 二維逆餘弦變換
- MATLAB - 向座標軸新增圖例
- MATLAB - 面向物件
- MATLAB - 面向物件程式設計
- MATLAB - 類和物件
- MATLAB - 函式過載
- MATLAB - 運算子過載
- MATLAB - 使用者自定義類
- MATLAB - 複製物件
- MATLAB - 代數
- MATLAB - 線性代數
- MATLAB - 高斯消元法
- MATLAB - 高斯-約旦消元法
- MATLAB - 簡化行階梯形
- MATLAB - 特徵值和特徵向量
- MATLAB - 積分
- MATLAB - 積分
- MATLAB - 二重積分
- MATLAB - 梯形法則
- MATLAB - 辛普森法則
- MATLAB - 其他
- MATLAB - 微積分
- MATLAB - 微分
- MATLAB - 矩陣的逆
- MATLAB - GNU Octave
- MATLAB - Simulink
- MATLAB - 有用資源
- MATLAB - 快速指南
- MATLAB - 有用資源
- MATLAB - 討論
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