MATLAB--MATLAB Onramp Practice

例1

Problem 1. Times 2 - START HERE

Try out this test problem first.

Given the variable x as your input, multiply it by two and put the result in y.

Examples:

 Input  x = 2
 Output y is 4
 Input  x = 17
 Output y is 34
function y = multiply_by_two(x)
    y = x * 2;
end

 可以将这个函数保存在一个名为 multiply_by_two.m 的文件中,然后在 MATLAB 中调用它。

例2

Problem 44943. Calculate Amount of Cake Frosting

Given two input variables r and h, which stand for the radius and height of a cake, calculate the surface area of the cake you need to put frosting on (all around the sides and the top).

Return the result in output variable SA.

(给定两个输入变量 r 和 h,它们分别代表蛋糕的底面半径和高度,计算需要涂抹霜的蛋糕的表面积(包括侧面和顶部)。将结果存储在输出变量 SA 中。)

function SA = calculate_surface_area(r, h)
    % 计算底面积
    base_area = pi * r^2;
    
    % 计算侧面积
    side_area = 2 * pi * r * h;
    
    % 总表面积
    SA = base_area + side_area;
end

 可以将这个函数保存在一个名为 calculate_surface_area.m 的文件中,然后在 MATLAB 中调用它。

例3

Problem 44944. Convert from Fahrenheit to Celsius

Given an input vector F containing temperature values in Fahrenheit, return an output vector C that contains the values in Celsius using the formula:

C = (F–32) * 5/9

(给定一个包含华氏温度值的输入向量 F,使用以下公式将其转换为摄氏温度值,并将结果存储在输出向量 C 中: C = (F–32) * 5/9)

function C = fahrenheit_to_celsius(F)
    % 将华氏温度转换为摄氏温度
    C = (F - 32) * 5/9;
end

 可以将这个函数保存在一个名为 fahrenheit_to_celsius.m 的文件中,然后在 MATLAB 中调用它

例4

Problem 44947. Find the Oldest Person in a Room

Given two input vectors:

  • name - user last names
  • age - corresponding age of the person

Return the name of the oldest person in the output variable old_name.

(给定两个输入向量:

name - 用户的姓氏 age - 对应人的年龄 将年龄最大的人的姓氏存储在输出变量 old_name 中。)

function old_name = find_oldest_person(name, age)
    % 找到年龄最大的人的姓氏
    [~, idx] = max(age);
    old_name = name{idx};
end

 可以将这个函数保存在一个名为 find_oldest_person.m 的文件中,然后在 MATLAB 中调用它。

例5

Problem 44951. Verify Law of Large Numbers

If a large number of fair N-sided dice are rolled, the average of the simulated rolls is likely to be close to the mean of 1,2,...N i.e. the expected value of one die. For example, the expected value of a 6-sided die is 3.5.

Given N, simulate 1e8 N-sided dice rolls by creating a vector of 1e8 uniformly distributed random integers. Return the difference between the mean of this vector and the mean of integers from 1 to N.

(如果投掷大量公平的 N 面骰子,模拟投掷的平均值可能接近于1,2,...N的平均值,即一个骰子的期望值。例如,一个6面骰子的期望值是3.5。 给定N,通过创建一个包含 1e8 个均匀分布的随机整数的向量来模拟 1e8 次 N 面骰子的投掷。返回这个向量的平均值与 1 到 N 的整数平均值

之间的差异。)

function difference = simulate_dice(N)
    % 模拟 N 面骰子投掷
    rolls = randi([1, N], 1e8, 1); % 创建包含 1e8 个均匀分布的随机整数的向量
    average_rolls = mean(rolls); % 计算向量的平均值
    expected_value = (1 + N) / 2; % 计算 1 到 N 的整数平均值
    difference = abs(average_rolls - expected_value); % 计算平均值之间的差异
end

可以调用这个函数并传入想要模拟的骰子的面数 N,然后它会返回模拟结果与期望值之间的差异。


1、randi(…)
  randi(N) 是生成(0,N]间均匀分布的伪随机数,并且数都是整数,所以每个数是位于1到N之间。它的表达形式有以下几种:

R = randi(iMax)            % 生成1:iMax之间的均匀分布随机数
R = randi(iMax,m,n)        % 生成m×n的1:iMax之间的均匀分布随机数
R = randi([iMin,iMax],m,n) % 生成m×n的iMin:iMax之间的均匀分布随机数
————————————————
原文链接:https://blog.csdn.net/FX677588/article/details/72811673

2.mean()

M = mean(A)
返回沿数组中不同维的元素的平均值。
如果A是一个向量,mean(A)返回A中元素的平均值。
如果A是一个矩阵,mean(A)将其中的各列视为向量,把矩阵中的每列看成一个向量,返回一个包含每一列所有元素的平均值的行向量。
如果A是一个多元数组,mean(A)将数组中第一个非单一维的值看成一个向量,返回每个向量的平均值。
————————————————                                   
原文链接:https://blog.csdn.net/wonengguwozai/article/details/51611270

3.Y = abs(X) 返回数组 X 中每个元素的绝对值。如果 X 是复数,则 abs(X) 返回复数的模。

例6.

Problem 44946. Solve a System of Linear Equations

Example:

If a system of linear equations in x₁ and x₂ is:

2x₁ + x₂ = 2

x₁ - 4 x₂ = 3

Then the coefficient matrix (A) is:

2 1

1 -4

And the constant vector (b) is:

2

3

To solve this system, use mldivide ( \ ):

x = A\b

Problem:

Given a constant input angle θ (theta) in radians, create the coefficient matrix (A) and constant vector (b) to solve the given system of linear equations in x₁ and x₂.

cos(θ) x₁ + sin(θ) x₂ = 1

-sin(θ) x₁ + cos(θ) x₂ = 1

(例子: 如果一个关于 x₁ 和 x₂ 的线性方程组是: 2x₁ + x₂ = 2 x₁ - 4x₂ = 3 那么系数矩阵 (A) 是: 2 1 1 -4 常数向量 (b) 是: 2 3 要解决这个方程组,使用 mldivide ( \ ): x = A\b 问题: 给定一个常数输入角度 θ(弧度制),创建系数矩阵 (A) 和常数向量 (b),以解决给定的线性方程组中的方程: cos(θ) x₁ + sin(θ) x₂ = 1 -sin(θ) x₁ + cos(θ) x₂ = 1)

% 定义输入角度 θ(弧度制)
theta = pi/4; % 例如,这里使用 π/4

% 创建系数矩阵 A
A = [cos(theta), sin(theta); -sin(theta), cos(theta)];

% 创建常数向量 b
b = [1; 1];

% 使用 mldivide ( \ ) 解决线性方程组
x = A\b;

 这段程序将计算给定角度 θ(弧度制)的线性方程组的解。

例7.

Problem 44948. Calculate a Damped Sinusoid

The equation of a damped sinusoid can be written as

y = A.ⅇ^(-λt)*cos(2πft)

where A, λ, and f are scalars and t is a vector.

Calculate the output sinusoid y given the inputs below:

  • lambda - λ
  • T - maximum value of t
  • N - number of elements in t

Assume A = 1 and f = 1 . The vector t should be linearly spaced from 0 to T, with N elements.

(这个阻尼正弦波的方程可以写成:

y = A.ⅇ^(-λt)*cos(2πft)

其中 A、λ 和 f 是标量,t 是向量。

给定以下输入,计算输出正弦波 y:

lambda - λ T - t 的最大值 N - t 中的元素数 假设 A = 1,f = 1。向量 t 应该从 0 到 T 线性间隔,含有 N 个元素。)

 以下是用MATLAB编写的程序来计算输出正弦波y:

function y = damped_sin(lambda, T, N)
    A = 1;
    f = 1;
    t = linspace(0, T, N);
    y = A * exp(-lambda * t) .* cos(2 * pi * f * t);
end

你可以调用这个函数,并传入 lambda、T 和 N 的值来计算输出正弦波y。


用法:linspace(x1,x2,N)  

功能:linspace是Matlab中的一个指令,用于产生x1,x2之间的N点行矢量。其中x1、x2、N分别为起始值、中止值、元素个数。若缺省N,默认点数为100。在matlab的命令窗口下输入help linspace或者doc linspace可以获得该函数的帮助信息。

例8.

Problem 44934. Plot Damped Sinusoid

Given two vectors t and y, make a plot containing a blue ( b ) dashed ( -- ) line of y versus t.

Mark the minimum value m of the vector y by adding a point to the plot. This point should be a red asterisk marker, and it must be added after the blue line.

(将两个向量t和y作图,包含一个蓝色的虚线y相对于t的线条。通过在图中添加一个点来标记向量y的最小值m。这个点应该是一个红色的星号标记,并且必须在蓝线之后添加。)

Return the minimum value of y as output m.

function m = plot_and_mark_min(t, y)
    % Plot y versus t with a blue dashed line
    plot(t, y, 'b--');
    hold on; % Keep the current plot
    % Find the minimum value of y and its index
    [min_y, min_index] = min(y);
    % Mark the minimum value with a red asterisk marker
    plot(t(min_index), min_y, 'r*');
    hold off; % Release the current plot
    % Set the output
    m = min_y;
end

 使用示例:

t = [1, 2, 3, 4, 5];
y = [5, 3, 2, 4, 1];
m = plot_and_mark_min(t, y);
disp(m); % 输出最小值

例9

Problem 44945. Calculate BMI

Given a matrix hw (height and weight) with two columns, calculate BMI using these formulas:

  • 1 kilogram = 2.2 pounds
  • 1 inch = 2.54 centimeters
  • BMI = weight(kg) / [height(m)]^2

The first column is the height in inches. The second column is the weight in pounds.

(给定一个包含两列的矩阵 hw(身高和体重),使用以下公式计算BMI: 1 千克 = 2.2 磅 1 英寸 = 2.54 厘米 BMI = 体重(千克)/ [身高(米)]^2 第一列是以英寸为单位的身高。第二列是以磅为单位的体重。)

% 输入的身高和体重矩阵
hw = [height_inches, weight_pounds]; % 假设已经定义了 height_inches 和 weight_pounds

% 转换单位:将英寸转换为米,将磅转换为千克
height_meters = hw(:,1) * 0.0254; % 英寸转米
weight_kg = hw(:,2) / 2.2; % 磅转千克

% 计算BMI
BMI = weight_kg ./ (height_meters .^ 2);

 这段程序将计算给定身高和体重矩阵的BMI值。

例10

Problem 44958. Crop an Image

A grayscale image is represented as a matrix in MATLAB. Each matrix element represents a pixel in the image. An element value represents the intensity of the pixel at that location.

Create a cropped image matrix Icrop using inputs given in the following order:

  1. I - Grayscale input image
  2. Rmin - Lowest row number to retain
  3. Cmin - Lowest column number to retain
  4. Rpix - Number of pixels along rows to retain
  5. Cpix - Number of pixels along columns to retain

For example, if your image was:

I = [1 2 3 4
     5 6 7 8]

And you called crop_image with inputs

Icrop = crop_image(I, 2, 2, 1, 3)

The output Icrop should be

[6 7 8]

(将灰度图像表示为 MATLAB 中的矩阵。每个矩阵元素代表图像中的一个像素。元素值表示该位置处像素的强度。

使用以下顺序提供的输入创建裁剪图像矩阵 Icrop:

I - 灰度输入图像 Rmin - 保留的最低行数 Cmin - 保留的最低列数 Rpix - 沿行保留的像素数 Cpix - 沿列保留的像素数

例如,如果你的图像是:

I = [1 2 3 4 5 6 7 8] 并且你调用 crop_image 函数时提供了以下输入:

Icrop = crop_image(I, 2, 2, 1, 3)

那么输出 Icrop 应该是:

[6 7 8]

function Icrop = crop_image(I, Rmin, Cmin, Rpix, Cpix)
    % 确定裁剪的结束行和列
    Rmax = Rmin + Rpix - 1;
    Cmax = Cmin + Cpix - 1;
    
    % 裁剪图像
    Icrop = I(Rmin:Rmax, Cmin:Cmax);
end

 你可以将此代码保存在名为 "crop_image.m" 的文件中,并在 MATLAB 中调用它,传入输入图像 I 以及裁剪的参数 RminCminRpixCpix

例11

Problem 44949. Find the Best Hotels

Given three input variables:

  • hotels - a list of hotel names
  • ratings - their ratings in a city
  • cutoff - the rating at which you would like to cut off

return only the names of those hotels with a rating of cutoff value or above as a column vector of strings good.

(给定三个输入变量: hotels - 酒店名称列表 ratings - 它们在一个城市中的评分 cutoff - 你想要截断的评分值 仅返回那些评分达到截断值或以上的酒店名称作为字符串列向量 good。)

function good = filter_hotels(hotels, ratings, cutoff)
    % 找到评分达到或超过截断值的酒店索引
    idx = ratings >= cutoff;
    
    % 从酒店列表中获取符合条件的酒店名称
    good = hotels(idx);
end

你可以将此代码保存在名为 "filter_hotels.m" 的文件中,并在 MATLAB 中调用它,传入酒店名称列表 hotels、它们的评分 ratings 以及截断值 cutoff

例12

Problem 44952. Find MPG of Lightest Cars

The file cars.mat contains a table named cars with variables Model, MPG, Horsepower, Weight, and Acceleration for several classic cars.

Load the MAT-file. Given an integer N, calculate the output variable mpg.

Output mpg should contain the MPG of the top N lightest cars (by Weight) in a column vector.

(文件 cars.mat 包含一个名为 cars 的表,其中包含几款经典汽车的变量 Model、MPG、Horsepower、Weight 和 Acceleration。 加载 MAT 文件。给定一个整数 N,计算输出变量 mpg。 输出 mpg 应包含前 N 辆最轻的汽车(按重量排序)的 MPG,以列向量形式呈现。)

function mpg = top_n_mpg(N)
    % 加载 cars.mat 文件
    load('cars.mat');
    
    % 按汽车重量排序
    sorted_cars = sortrows(cars, 'Weight');
    
    % 获取前 N 辆最轻的汽车的 MPG
    mpg = sorted_cars(1:N, 'MPG');
    mpg = table2array(mpg);
end

你可以将此代码保存在名为 "top_n_mpg.m" 的文件中,并在 MATLAB 中调用它,传入整数 N 来获取前 N 辆最轻的汽车的 MPG。


1.load()

load(filename) 从 filename 加载数据。

  • 如果 filename 是 MAT 文件,load(filename) 会将 MAT 文件中的变量加载到 MATLAB® 工作区。

  • 如果 filename 是 ASCII 文件,load(filename) 会创建一个包含该文件数据的双精度数组。

示例

load(filename,variables) 加载 MAT 文件 filename 中的指定变量。

示例

load(filename,'-ascii') 将 filename 视为 ASCII 文件,而不管文件扩展名如何。

load(filename,'-mat') 将 filename 视为 MAT 文件,而不管文件扩展名如何。

load(filename,'-mat',variables) 加载 filename 中的指定变量。

示例

S = load(___) 使用前面语法组中的任意输入参数将数据加载到 S 中。

  • 如果 filename 是 MAT 文件,则 S 是结构数组。

  • 如果 filename 是 ASCII 文件,则 S 是包含该文件数据的双精度数组。

2.sortrows()

sortrows有三种用法:
B = sortrows(A)
B = sortrows(A,column)
[B,index] = sortrows(A,...)

我们先创建一个矩阵
A=floor(gallery('uniformdata',[6 7],0)*100);
A(1:4,1)=95;  A(5:6,1)=76;  A(2:4,2)=7;  A(3,3)=73
A =
    95    45    92    41    13     1    84
    95     7    73    89    20    74    52
    95     7    73     5    19    44    20
    95     7    40    35    60    93    67
    76    61    93    81    27    46    83
    76    79    91     0    19    41     1

默认依据第一列的数值按升序移动每一行,如果第一列的数值有相同的,依次往右比较。例:
B = sortrows(A)
B =
    76    61    93    81    27    46    83
    76    79    91     0    19    41     1
    95     7    40    35    60    93    67
    95     7    73     5    19    44    20
    95     7    73    89    20    74    52
    95    45    92    41    13     1    84

或是从某一列开始比较数值并按升序排序,例:
C = sortrows(A,2)
C =
    95     7    73    89    20    74    52
    95     7    73     5    19    44    20
    95     7    40    35    60    93    67
    95    45    92    41    13     1    84
    76    61    93    81    27    46    83
    76    79    91     0    19    41     1

亦可以从某一列开始以降序排列,例:
D = sortrows(A, -4)
D =
    95     7    73    89    20    74    52
    76    61    93    81    27    46    83
    95    45    92    41    13     1    84
    95     7    40    35    60    93    67
    95     7    73     5    19    44    20
    76    79    91     0    19    41     1

如果要求每一列都按照升序排列
E=sort(A)

如果要求每一列都按照降序排列
F=-sort(-A)
————————————————                     
原文链接:https://blog.csdn.net/smf0504/article/details/51810642

3.table2array()

table2array函数是将表转换为同构数组。

语法
A = table2array(T)
输入参数

        输入表,指定为表。T 中的所有变量都必须具有适合水平串联的大小和数据类型。具体而言,维度大于 2 的所有变量的大小必须匹配。

如果 T 是一个每个变量具有一列的 m×n 表,则每个变量都将成为A中的一列,并且 A 是一个 m×n 数组。

如果 T 中的变量包含多列,这些变量将成为 A 中的多列,并且A的大小大于T的大小。

如果 T 包含二维以上的变量,则 A 的维数与变量维数相同。

提示

table2array 水平串联 T 中的变量以创建 A。如果 T 中的变量为元胞数组,则 table2array 不会串联其内容,并且 A 也为元胞数组,等效于 table2cell(T)。要创建包含元胞数组变量的内容的数组,请使用 cell2mat(table2cell(T))。

table2array(T) 等效于 T{:,:}。

算法:如果 T 包含的变量具有适合水平串联的不同数据类型,table2array 将会创建一个具有主导数据类型的同构数组 A。例如,如果 T 包含 double 和 single 数值数据,table2array(T) 将返回数据类型为 single 的数组。

说明
A = table2array(T) 将表 T 转换为同构数组 A。

示例
将数值数据的表转换为数组
        创建一个包含数值数据的表 T。

T = table([1;2;3],[2 8; 4 10; 6 12],[3 12 21; 6 15 24; 9 18 27],...
    'VariableNames',{'One' 'Two' 'Three'})
T=3×3 table
    One      Two          Three    
    ___    _______    _____________
 
     1     2     8    3    12    21
     2     4    10    6    15    24

————————————————                   
原文链接:https://blog.csdn.net/jk_101/article/details/110939321


例13

Problem 44950. Calculate Inner Product

Given two input matrices, x and y, check if their inner dimensions match.

  • If they match, create an output variable z which contains the product of x and y
  • Otherwise, z should contain a custom string message

Example:

x = [1 2;3 4]
y = [5;6]

z = [17;39]

x = [1 2 3;4 5 6]
y = [2 5;3 6]

z = "Have you checked the inner dimensions?"

OR

z = "The inner dimensions are 3 and 2. Matrix multiplication is not possible"

-------------------------------------------------------------------------------------------------------------------------------------------------------------

NOTE - An example of combining numbers and strings together is shown below:

x = "The sum of " + 4 + " and " + 3 + " equals " + 7
x = 
      "The sum of 4 and 3 equals 7"

(给定两个输入矩阵 x 和 y,检查它们的内部维度是否匹配。

如果匹配,创建一个输出变量 z,其中包含 x 和 y 的乘积。 否则,z 应包含一个自定义字符串消息。 示例:

x = [1 2;3 4] y = [5;6] z = [17;39]

x = [1 2 3;4 5 6] y = [2 5;3 6] z = "你检查过内部维度吗?"

或者

z = "内部维度为 3 和 2。矩阵乘法不可能进行。"


注意——下面展示了一个将数字和字符串结合在一起的示例:

x = "The sum of " + 4 + " and " + 3 + " equals " + 7 x =       "4 和 3 的和等于 7"

function z = matrixMultiplication(x, y)
    % 检查内部维度是否匹配
    if size(x, 2) == size(y, 1)
        % 计算矩阵乘积
        z = x * y;
    else
        % 创建自定义消息
        message = "内部维度为 " + size(x, 2) + " 和 " + size(y, 1) + "。矩阵乘法不可能进行。";
        z = message;
    end
end

% 示例用法:
x1 = [1 2; 3 4];
y1 = [5; 6];
z1 = matrixMultiplication(x1, y1);

x2 = [1 2 3; 4 5 6];
y2 = [2 5; 3 6];
z2 = matrixMultiplication(x2, y2);

你可以调用 matrixMultiplication 函数并传入两个矩阵 x 和 y,它会返回 z,根据内部维度是否匹配,z 将包含矩阵乘积或自定义消息。

例14

Problem 44960. Rescale Scores

Each column (except last) of matrix X contains students' scores in a course assignment or a test. The last column has a weighted combination of scores across all the assignments and tests.

Replace the elements in the last column of this matrix with a GPA calculated based on the following scale:

Score GPA

91 - 100 3 - 4

81 - 90 2 - 3

71 - 80 1 - 2

61 - 70 0 - 1

Below 60 0

Assume that no student in this class has scored below 60. Also note that the mapping in the range [60, 100] is linear.

See <https://www.mathworks.com/help/matlab/ref/rescale.html>

Example

Input:

100 90 95 95

70 50 60 60

80 70 90 80

Output:

100 90 95 3.5

70 50 60 0.0

80 70 90 2.0

(每一列(除了最后一列)的矩阵 X 包含学生在课程作业或测试中的分数。最后一列包含所有作业和测试分数的加权组合。 将该矩阵的最后一列元素替换为根据以下比例尺计算的 GPA: 分数 GPA
91 - 100 3 - 4
81 - 90 2 - 3
71 - 80 1 - 2
61 - 70 0 - 1
60以下 0 假设此班级中没有学生的分数低于60。还请注意,在范围[60, 100]内的映射是线性的。 请参阅 Scale range of array elements - MATLAB rescale 示例 输入: 100 90 95 95 70 50 60 60 80 70 90 80 输出: 100 90 95 3.5 70 50 60 0.0 80 70 90 2.0)

function updatedMatrix = updateGPA(matrix)
    % 提取分数列
    scores = matrix(:, 1:end-1);
    
    % 计算加权平均分数
    weightedScores = mean(scores, 2);
    
    % 根据分数计算GPA
    gpa = rescale(weightedScores, 0, 4, 60, 100);
    
    % 更新最后一列
    updatedMatrix = [matrix(:, 1:end-1), gpa];
end

% 示例用法:
inputMatrix = [
    100  90  95  95;
    70  50  60  60;
    80  70  90  80
];

outputMatrix = updateGPA(inputMatrix);
disp(outputMatrix);

 这个程序首先提取了除了最后一列的分数列,然后计算了加权平均分数。接着使用 rescale 函数将分数映射到对应的GPA范围。最后,更新了矩阵的最后一列为计算得到的GPA。


rescale():

rescale

数组元素的缩放范围

全页折叠

语法

R = rescale(A)

R = rescale(A,l,u)

R = rescale(___,Name,Value)

说明

示例

R = rescale(A) 根据 A 中所有元素的最小值和最大值,将 A 中的所有元素缩放到区间 [0, 1]。输出数组 R 的大小与 A 相同。

示例

R = rescale(A,l,u) 将 A 中的所有元素缩放到区间 [l u]

示例

R = rescale(___,Name,Value) 支持上述语法中的任何输入参量组合,且可使用一个或多个名称-值参量指定选项。例如,rescale(A,"InputMin",5) 在缩放到区间 [0, 1] 之前,将 A 中小于 5 的所有元素设置为 5。


例15

Problem 672. Longest run of consecutive numbers

Given a vector a, find the number(s) that is/are repeated consecutively most often. For example, if you have

a = [1 2 2 2 1 3 2 1 4 5 1]

The answer would be 2, because it shows up three consecutive times.

If your vector is a row vector, your output should be a row vector. If your input is a column vector, your output should be a column vector. You can assume there are no Inf or NaN in the input. Super (albeit non-scored) bonus points if you get a solution that works with these, though.

(给定一个向量a,找出连续出现最多次数的数字。例如,如果你有:

a = [1 2 2 2 1 3 2 1 4 5 1]

答案将是2,因为它连续出现了三次。

如果你的向量是行向量,你的输出应该是一个行向量。如果你的输入是列向量,你的输出应该是一个列向量。你可以假设输入中没有Inf或NaN。如果你能得到一个能处理这些情况的解决方案,将会有超级奖励点(尽管不计分)。)

function result = most_repeated_consecutive(a)
    % Find unique consecutive elements
    unique_elements = [a(diff(a) ~= 0), a(end)];
    % Calculate the length of consecutive sequences
    consecutive_lengths = diff(find(diff([0, a, 0])));
    % Find the maximum consecutive length
    max_length = max(consecutive_lengths);
    % Find the element(s) with the maximum consecutive length
    result = unique_elements(consecutive_lengths == max_length);
end

 使用示例:

a = [1 2 2 2 1 3 2 1 4 5 1];
result = most_repeated_consecutive(a);
disp(result); % 输出结果

1.diff()

diff(X) , 求函数X的一阶导数;

diff(X, n) , 求函数X的n阶导 (n是具体整数);

diff(X,变量名), 求函数X的偏导数(对谁求偏导数,变量名就是谁)

diff(X, 变量名,n) ,求函数X的n阶偏导数。

2.find() 

(1)向量

返回非零元素下标
find(vector

 x=[1  2 3 0 0 6  7 8 9];
find(x)

ans =

     1     2     3     6     7     8     9

 返回前k个非零元素的下标
find(vector,k)
或find(vector,k,‘first’)

x=[1  2 3 0 0 6  7 8 9];
find(x,2)

ans =

     1     2
 
find(x,2,'first')

ans =

     1     2

 matlab中find()函数用法_matlab中find函数的用法-CSDN博客


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值