making column of zero matrix equal to one based on another matrix

Hi ,
I have a zero matrix (A) of size mxn where m in number of observation and n number of feature. I have another non zero matrix (B) of size (mx1) where numbers respresent column in matrix A which should be replaced by one.
How to do this ?

 採用された回答

Jos (10584)
Jos (10584) 2014 年 2 月 25 日

0 投票

Perhaps you want to set in a row K of A, the entry in column B(k) to one?
%data
A = zeros(3,3)
B = [3; 1; 2] % column index
% engine
R = 1:numel(B) % row index
idx = sub2ind(size(A), R, B) % convert to linear indices into A
A(idx) = 1
Note that the statement A(R,B) = 1 does not do what you might think it should do.

5 件のコメント

Sukuchha
Sukuchha 2014 年 2 月 25 日
thnks everyone, may be i should have phrased it more clearly. Jos understood what i wanted and it worked. Thanks every one. I knew this function sub2ind but it didnot cross my mind.
Image Analyst
Image Analyst 2014 年 2 月 25 日
Strange, I just copied and pasted that code and it errored out:
Error using sub2ind (line 47)
The subscript vectors must all be of the same size.
Error in test2 (line 6)
idx = sub2ind(size(A), R, B) % convert to linear indices into A
Sukuchha
Sukuchha 2014 年 2 月 25 日
編集済み: Sukuchha 2014 年 2 月 25 日
yes, the error you got is right because R in above is [1,3] and B is [3,1].
The exact code that i adapted is: %data
A = zeros(3,3)
B = [3; 1; 2] % column index
% engine
R = 1:numel(B) % row index
idx = sub2ind(size(A), R, B') % convert to linear indices into A
A(idx) = 1
thanx once again.
Jos (10584)
Jos (10584) 2014 年 2 月 26 日
@Image Analyst My apologies, I should have tested the code properly, rather than write it down theoretically.
@Sukuchha, I am glad you solved this issue yourself.
Image Analyst
Image Analyst 2014 年 2 月 26 日
No problem - I sometimes post untested code off the top of my head also, and mistakes do happen. Actually I'm kind of impressed that you (1) actually figured out what he meant, and (2) thought up that somewhat cryptic solution off the top of your head.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 2 月 25 日

0 投票

Did you try
A(:, B) = 1;

3 件のコメント

Sukuchha
Sukuchha 2014 年 2 月 25 日
hi image analyst, your suggestion didnot work.
A = zeros(3,3)
B = [3;1;2]
A(:,B)=1;
As you can see every element in A is one.
Paul
Paul 2014 年 2 月 25 日
編集済み: Paul 2014 年 2 月 25 日
Well, that is what you said you want. Since all three the column indices are present in B, all values of A are set to 1.
Image Analyst
Image Analyst 2014 年 2 月 25 日
編集済み: Image Analyst 2014 年 2 月 25 日
I agree with Paul. You said any column number of A that shows up in B should have that entire column of A set to 1. For example:
A = randi(9, 5, 8) % Random integers
B = [1,3,7]; % Set columns 1, 3, and 7 to all 1's.
A(:, B) = 1

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by