select one element in each row but not the same column

조회 수: 1 (최근 30일)
Hardi Mohammed
Hardi Mohammed 2019년 2월 7일
댓글: Hardi Mohammed 2019년 2월 15일
Hi everyone I have matrices 3*3 A= [1 2 3; 4 5 6; 7 8 9] and B=zeros(3,3) I want to choose one element in each row, for example if i choose A(1,1), the matrice of B(1,1) change its value to 1,
  댓글 수: 2
madhan ravi
madhan ravi 2019년 2월 7일
Give an example of your expected result.
Hardi Mohammed
Hardi Mohammed 2019년 2월 15일
[1 0 0
0 1 0
0 0 1]
or [ 1 0 0
0 0 1
0 1 0]
somthing like the above

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

채택된 답변

Jos (10584)
Jos (10584) 2019년 2월 7일
A= [1 2 3; 4 5 6; 7 8 9]
ix = logical(eye(size(A))) % one element per row
ix = ix(:,randperm(size(ix,2))) % randomize accross columns
% this ix matrix is now already your B, I think
B = zeros(size(A)) ; % create B
B(ix) = 1 ;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by