MATLAB--Basics on Vectors(向量的基础知识)

例1.

Problem 3. Find the sum of all the numbers of the input vector

Find the sum of all the numbers of the input vector x.

 Examples:
Input  x = [1 2 3 5]
Output y is 11
Input  x = [42 -1]
Output y is 41

代码实现:

x1=[1,2,3,5];
y1=sum(x1)

例2.

Problem 6. Select every other element of a vector

Write a function which returns every other element of the vector passed in. That is, it returns the all odd-numbered elements, starting with the first.

Examples:

Input x = [1 3 2 4 3 5]

Output y is [1 2 3]

Input x = [5 9 3 2 2 0 -1]

Output y is [5 3 2 -1]

代码实现:

%%02
x=[5,9,3,2,2,0,-1];
y=[];
n=length(x);
k=0;
for i=1:n
    if mod(i,2)~=0  %%  mod(m,n)取余数
        k=k+1;
        y(k)=x(i);
    end
end
y

例3.

Problem 247. Arrange Vector in descending order

If x=[0,3,4,2,1] then y=[4,3,2,1,0]

代码实现:

%03
x = [0, 3, 4, 2, 1];
y = sort(x, 'descend');%将向量x降序排列
disp(y)

例4.

Problem 135. Inner product of two vectors

Find the inner product of two vectors.

代码实现:

%04
y=[1,2,4,5,7];
x = [0, 3, 4, 2, 1];
A1=x.*y 
A2=x*y

运行结果:

A1 =

     0     6    16    10     7

错误使用  * 
用于矩阵乘法的维度不正确。请检查并确保第一个矩阵中的列数与第二个矩阵中的行数匹配。要单独对矩阵的每个元素进行运算,请使用 TIMES (.*)执行按元素相乘。

MATLAB.*和*的区别:

  • 点运算是处理元素之间的运算
  • 直接*在矩阵计算中只能处理符合矩阵运算法则的运算
  • 在对数值或矩阵计算时,“.*”和“*”其实是没有区别的。
    例:对于矩阵A=[a b c d],2.*A=[2*a  2*b  2*c  2*d]=2*A
    >> 2.*A
    ans =
         4    68    64     8
    >> 2*A
    ans =
         4    68    64     8
    ———————————————
    原文链接:https://blog.csdn.net/u013346007/article/details/54583271

例5

Problem 624. Get the length of a given vector

Given a vector x, the output y should equal the length of x.

(给定一个向量 x,输出 y 应该等于向量 x 的长度)

代码实现:

x = [0, 3, 4, 2, 1];
y=length(x)

y =     5

例6.

Problem 1107. Find max

Find the maximum value of a given vector or matrix.

代码实现:

x = [0, 3, 4, 2, 1];
max_x=max(x)
A=[2,3;4,8];
max_A=max(max(A))

>>

max_x =   4
max_A =   8

矩阵求最大、最小值用max、min函数。
max(A)、min(A):返回行向量,求每列最大、最小值;
max(A,B)、min(A,B):返回一个A,B中比较大、较小元素组成的矩阵;
max(A,[],dim)、min(A,[],dim):dim=1,比较A的列;dim=2,比较A的行;
如A=1 2 3
4 5 6
max(A,[],1)=[4,5,6]; max(A,[],2)=[3,6]’;
[C,I]=max(A)、 [C,I]=minA):C表示A每列的最大、最小值,I表示最大、最小值的下标;
max(max(A))、 min(min(A)):矩阵A所有元素的最大、最小值。
————————————————
原文链接:https://blog.csdn.net/qq_38549334/article/details/94647487

例7

Problem 605. Whether the input is vector?

Given the input x, return 1 if x is vector or else 0.

代码实现:

function result = isVector(x)
    if isvector(x)
        result = 1;
    else
        result = 0;
    end
end
%你可以通过调用这个函数并传入向量 x 来判断 x 是否是一个向量。例如:
x = [1, 2, 3];
output = isVector(x);
disp(output); % 这将输出 1,因为 x 是一个向量

x = 5;
output = isVector(x);
disp(output); % 这将输出 0,因为 x 不是一个向量

isvector()函数的使用 

例8

Problem 2631. Flip the vector from right to left

Flip the vector from right to left.

Examples

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

Request not to use direct function.

代码实现:

%要将向量从右到左翻转,你可以使用MATLAB中的fliplr函数。以下是相应的MATLAB程序:
x = [1:5];
y = fliplr(x);
disp(y);
%这段代码将输出:
5     4     3     2     1
%它将向量x从右到左翻转,并将结果存储在向量y中。
% here's a MATLAB program to flip a vector from right to left without using the direct function fliplr:
x = [1:5];
n = length(x);
y = zeros(1, n); % Preallocate memory for y
for i = 1:n
    y(i) = x(n - i + 1);
end
disp(y);

%This code will output:
5     4     3     2     1

fliplr()函数的使用 

例9.

Problem 3076. Create a vector

Create a vector from 0 to n by intervals of 2.(通过间隔为 2 创建一个从 0 到 n 的向量。)

代码实现:

n=6;
x=[0:2:n]
>>
  x= 0     2     4     6

例10

Problem 1024. Doubling elements in a vector

Given the vector A, return B in which all numbers in A are doubling. So for:

A = [ 1 5 8 ]

then

B = [ 1 1 5 5 8 8 ]

代码实现:

A = [1, 5, 8];
B = repelem(A, 2);
disp(B);
%这段代码将输出:
1     1     5     5     8     8
%它将向量 A 中的每个数字重复两次,并将结果存储在向量 B 中。

repelem ()函数用于将数组中的元素重复指定次数。在这种情况下,它将向量中的每个元素重复指定的次数。

例11

Problem 42651. Vector creation

Create a vector using square brackets going from 1 to the given value x in steps on 1.

Hint: use increment.("使用方括号创建一个向量,从 1 开始,以步长为 1 直到给定值 x。 提示:使用增量。")

代码实现:

%以下是相应的 MATLAB 程序:
function vector = createVector(x)
    vector = [1:1:x];
end
%你可以通过调用这个函数并传入值 x 来创建所需的向量。例如:

x = 5;
result = createVector(x);
disp(result); % 这将输出 [1 2 3 4 5]
%这个程序使用了方括号和步长为 1 的增量来创建从 1 到给定值 x 的向量。

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值