You can use a nested loop to iterate over each element in the matrix, and use a conditional statement to check if the element is negative. Here's an example code block:
main.m471 chars23 lines
In this example, we define a 3x3 matrix A
. We then use the size
function to get the number of rows and columns in A
, and use a nested loop to iterate over each element in the matrix. We use the conditional statement if A(i,j) < 0
to check if the current element is negative, and if it is, we add it to the neg_nums
vector using neg_nums(end+1) = A(i,j)
. Finally, we display the contents of the neg_nums
vector using the disp
function.
gistlibby LogSnag