Using nested loop to check for whole numbers inside a matrix

조회 수: 5 (최근 30일)
Jose Grimaldo
Jose Grimaldo 2019년 10월 20일
답변: Devineni Aslesha 2019년 10월 23일
Im trying to find any values that are not whole numbers inside a 3x3 matrix, but i have to used nested loops
This my code
x=[1 2.5 4;3.2 6 9;5.2 6 7]
w=mod(x,1)~=0; %checking for whole numbers in the matrix
d=x(w); %this are the values that failed the whole number test
[r,c]=find(w); %location of those values
How would i used nested loops to check every value in the matrix for values that are not whole numbers?
  댓글 수: 1
Daniel M
Daniel M 2019년 10월 21일
Why do you want to use loops, you have just solved it in a more efficient way?

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

답변 (1개)

Devineni Aslesha
Devineni Aslesha 2019년 10월 23일
To check for whole numbers inside a matrix using nested loop, use the code as shown below.
x = [1 2.5 4;3.2 6 9;5.2 6 7];
[rx,cx] = size(x);
for i = 1:rx
for j = 1:cx
w(i,j) = mod(x(i,j),1)==0;
end
end
[r,c] = find(w);
Refer to the following doc for more details.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by