I think this is vectorizing

조회 수: 1 (최근 30일)
Spaceman
Spaceman 2024년 4월 8일
댓글: Spaceman 2024년 4월 9일
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.

채택된 답변

Venkat Siddarth Reddy
Venkat Siddarth Reddy 2024년 4월 8일
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
Spaceman
Spaceman 2024년 4월 9일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by