MATLAB Answers

How to choose values ​​in the second and third column corresponding to the drawn numbers?

1 ビュー (過去 30 日間)
hawk5577
hawk5577 2018 年 11 月 15 日
Answered: Bruno Luong 2018 年 11 月 17 日
%N X Y
AA=[ 1 0 4
2 1 5
21 4 6
81 3 7
92 7 8
73 6 4
65 3 3
36 5 4
16 6 5
6 7 4]
A=AA(:,1);
disp(A);
b=(A(randperm(size(A,1),3),1))
disp(b);
% How to choose values ​​in the second and third column corresponding to the drawn numbers?
for i=1:3 %This solution give me error - Index in position 1 exceeds array bounds (must not exceed 10).
c=b(i,1);
disp(AA(c,2));
disp(AA(c,3));
end

  0 件のコメント

サインイン to comment.

採用された回答

Arunkumar M
Arunkumar M 2018 年 11 月 17 日
編集済み: madhan ravi 2018 年 11 月 17 日
Error occurs because with c you are finding the element which is a part of first column in AA. But this element is not the index. So you have to find the index where it is located and then pull out second and third column values.
for i=1:3
c=b(i,1);
temp1 = find(A == c);
temp2 = temp1(1,1); % in case multiple values are returned in temp1.
disp(AA(temp2,2));
disp(AA(temp2,3));
end

  2 件のコメント

hawk5577
hawk5577 2018 年 11 月 17 日
Error:
Index in position 1 exceeds array bounds.
Error in smiec4 (line 22)
temp2 = temp1(1,1); % in case multiple values are returned in temp1.

サインイン to comment.

More Answers (1)

Bruno Luong
Bruno Luong 2018 年 11 月 17 日
Why make so complicated? RANDPERM returns the position, store and use it rather than trying to recover it.
AA=[ 1 0 4
2 1 5
21 4 6
81 3 7
92 7 8
73 6 4
65 3 3
36 5 4
16 6 5
6 7 4]
p = randperm(size(A,1),3);
b = AA(p,1)
AA(p,:)

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by