How to show my output from matrix?

조회 수: 3 (최근 30일)
Grace
Grace 2014년 5월 7일
댓글: David Sanchez 2014년 5월 7일
Hi everyone, i have
c= 1 1 1; 2 4 8; 3 9 27; 4 16 64
I extracted each row by c(1,:), c(2,:), c(3,:) and c(4,:). I want to find which row of the greatest common divisor (gcd) value with 5 is equal to 1 for each of the elements in the row vector.
ind=find(gcd(c(1,:),5)==1)
The answer gives me 1 2 3 if this command is used. I want the output is 1 as I am only interested in which row gives me all gcd value is 1.
How can i do this?
Thanks.

채택된 답변

David Sanchez
David Sanchez 2014년 5월 7일
For all your rows:
gcd(c(k,:),5) = [1 1 1];
Then,
find(gcd(c(3,:),5)==1) = [1 2 3]
since all the elements in the first operation equal 1 (for all rows)
You want to the first element fulfilling the condition:
ind=find(gcd(c(3,:),5)==1,1)
ind =
1
  댓글 수: 2
Grace
Grace 2014년 5월 7일
Hi David,
I wish to find which row gives me all the value is 1.
Since for all my rows: gcd(c(k,:),5)=[1 1 1];
Then the output I want to get should 1 2 3 4 because all 4 rows give me [1 1 1].
For example, if the first row and third row give me [1 1 1]. then the output should be 1 3.
The output ind shows which row gives me [1 1 1].
David Sanchez
David Sanchez 2014년 5월 7일
more clear now. The [1 2 3] you get from your commands correspond to the elements within each row and not to the rows of C, which is what you want.
You can do something like this though:
c= [1 1 1; 2 5 8; 3 9 27; 4 16 64];
N = size(c,1);
idx = zeros(N,1);
for k=1:N
v = all(gcd(c(k,:),5) == 1); % v = 1 if all the elements of row == 1
if v
idx(k) = k;
end
end
idx will contain the rows that fulfil your condition

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by