I think this is vectorizing
Show older comments
Given: Write a code that prompts user to enter a number of rows and columns. Create a matrix, with the provided size that has random integers between -10 and 10.
Find: How do I have MATLAB individually look at each value in the matrix and determine if the value is negative, and if it is, add one to a variable used to keep track of the negative values? (Otherwise do nothing). Then display to the user the number of negative values in the matrix.
Issue: I am running into syntax error brain. In my head this code works, but it's not what MATLAB thinks is right.
My solution: Once I figure out how to get my random numbers to properly generate I can tackle the tracking system... I might be going about this all wrong, though.
R=input('Enter the number of rows: ');
C=input('Enter the number of columns: ');
MAT=[R(randi([-10,10]),C(randi([-10,10])))]; % Getting an error here, syntax?
for x=1:R<1
disp('Negative')
for y=1:C<1
disp('Negative')
end
end
% Then create a variable neg and somehow??? do neg=neg+1 every time there is a
% negative value found in the above for loop.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!