I think this is vectorizing

1 visualización (últimos 30 días)
Spaceman
Spaceman el 8 de Abr. de 2024
Comentada: Spaceman el 9 de Abr. de 2024
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.

Respuesta aceptada

Venkat Siddarth Reddy
Venkat Siddarth Reddy el 8 de Abr. de 2024
Hi Kyle,
In the above code you are trying to use the variables "R" and "C" as the functions to create a matrix of dimensions of R and C. However, these are not functions but variables with user-defined values.
To create a matrix of random numbers of above dimensions you can pass the dimension values to the "randi" function itself.
%Let the values of R,C be 3,4
R=3;
C=4;
MAT= randi([-10 10],R,C)
MAT = 3x4
4 -3 0 -7 -7 -9 -5 3 7 4 -7 -8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And to find the count of negative numbers.You can either loop through each element of the matrix and check for the value or you can apply a boolean matrix operations and count for all the valid elements. Following example shows both the methods
%Method-1 Iterate through each element
negCount=0;
for i=1:R
for j=1:C
if(MAT(i,j)<0)
negCount=negCount+1;
end
end
end
negCount
negCount = 7
%Method-2 Matrix boolean operation
%Generating a boolean matrix on condition whether the element is less than zero or not.
negCount=nnz(MAT<0) %nnz - gives number of non zero elements in the matrix.
negCount = 7
To learn more about randi with these arguments and nnz function, refer to the following documentation:
I hope it helps!.
  1 comentario
Spaceman
Spaceman el 9 de Abr. de 2024
Eureka! Of course, this makes complete sense. I normally code after going to classes all morning and working 9 hours, so I'm often pretty mentally and physically exhausted by the time I get around to practicing. But this makes complete sense now that I see it. I was unsure if I could state 2 for loops before I passed it an if statement. Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by