필터 지우기
필터 지우기

make a logical matrix from an available array of numbers.

조회 수: 1 (최근 30일)
Furqan Haider
Furqan Haider 2014년 10월 8일
댓글: Guillaume 2014년 10월 8일
i get an array of numbers in which minimum number is 1 and maximum number is 7 , total elements in that array are 6, e.g.
A=[2 5 6 2 7 4]
I want to make a logical matrix of order 7*6 in such a way that there is only one '1' in each column and all other entries of that column are '0'. Now the numbers in array A indicate the row number at which binary '1' is to be placed. e.g. for above provided array, my logical matrix would be:
b =
0 0 0 0 0 0
1 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 1
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 0 1 0
kindly generalize me this program. for any random array 'A'.
Thank you.

채택된 답변

Guillaume
Guillaume 2014년 10월 8일
cols = 1:numel(A);
B = zeros(max(A), numel(A));
B(sub2ind(size(B), A, cols)) = 1
  댓글 수: 2
Furqan Haider
Furqan Haider 2014년 10월 8일
if there is no 7 in array A, then your code gives me 6*6 matrix, which I dont want. I need 7*6 matrix at any cost, whether there is 7 in the array or not.
Kindly suggest, and thank you.
Guillaume
Guillaume 2014년 10월 8일
Well, it's trivial to modify my example. Replace 2nd line with:
B = zeros(7,6);
You could have worked that out for yourself.

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

추가 답변 (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