matlab中如何改变数组中的某些元素

  1. Use structured programming to:
  2. Find the entries in matrix A that are negative
  3. Store these entries’ position in a matrix B
  4. Change the values of these entries to zero
    A = [ 0 1 4 9 14 25 34 49 64 ]

1. for循环

.m文件

A = [0, -1, 4; 9, -14, 25; -34, 49, 64]
for n = 1:3
    for m = 1:3
        if A(n,m) < 0
            A(n,m) = 0;
        end
    end
end
B = A

2. 布尔数组引用法

.m文件

A = [0, -1, 4; 9, -14, 25; -34, 49, 64]
A(A<0) = 0;
B = A

猜你喜欢

转载自blog.csdn.net/zhao416129/article/details/82715692