MATLAB Answers

select one element in each row but not the same column

조회 수: 5(최근 30일)
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 7 Feb 2019
Give an example of your expected result.
Hardi Mohammed
Hardi Mohammed 15 Feb 2019
[1 0 0
0 1 0
0 0 1]
or [ 1 0 0
0 0 1
0 1 0]
somthing like the above

로그인 to comment.

채택된 답변

Jos (10584)
Jos (10584) 7 Feb 2019
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 ;

  댓글 수: 1

Hardi Mohammed
Hardi Mohammed 15 Feb 2019
Thank you very much.

로그인 to comment.

추가 답변(0개)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

태그

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


Translated by