Matrix manipulation from one to another one

How to generate
A= [ 1 0 3 0 0; ...
0 7 0 0 10; ...
0 0 0 14 15]
from
B = [1 2 3 4 5; ...
6 7 8 9 10; ...
11 12 13 14 15]
[EDITED, Jan, Code formatted]

3 件のコメント

Akira Agata
Akira Agata 2017 年 11 月 28 日
What is the rule to determine whether keeping the number (e.g 1 -> 1) or changing to zero (e.g 2 -> 0) ?
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 28 日
A needs to get generated in a random manner such that numbers in B should exist in A in any column whereas the rest of the numbers in the same column should be as zero
Jan
Jan 2017 年 11 月 28 日
@Prabha Kumaresan: Did you see, that your complete code appeared in one single line? Then the readers do not have any chance to see, that you are talking about matrices, because it looked like a vector.
I've selected your code with the mouse and hit the "{} Code" button. In addition I've inserted "; ..." for clarity. Please format the code by your own in future questions. And read your question after posting it to fix details, which cannot be understood by the readers. Thanks.

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

 採用された回答

Akira Agata
Akira Agata 2017 年 11 月 28 日

0 投票

Here is the B = [multiple rows * multiple columns] example.
B = reshape(1:64,8,8); % Create 8x8 sample matrix
idx = rand(size(B)) > 0.5; % Randomly select whether keep it or change to zero
A = B;
A(idx) = 0;

5 件のコメント

Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 28 日
but i want to have only one number in each column rest of the other place where the number holds should be 0.
Akira Agata
Akira Agata 2017 年 11 月 29 日
OK. Then, how about the following code?
B = [1 2 3 4 5; ...
6 7 8 9 10; ...
11 12 13 14 15];
sz = size(B);
rowPos = randi(sz(1),sz(2),1);
linInd = sub2ind(sz, rowPos, (1:sz(2))');
idx = false(sz);
idx(linInd) = true;
A = B;
A(~idx) = 0;
The output is:
>> A
A =
1 0 0 4 0
0 0 8 0 0
0 12 0 0 15
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 29 日
thanks for your code.
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 29 日
How can i plot the graph of A = 1 0 0 4 0; 0 0 8 0 0; 0 12 0 0 15; could u help me?
Stephen23
Stephen23 2017 年 11 月 29 日
@Prabha Kumaresan: please format your code correct so that it is readable. Jan Simon has already explained how you can do this.

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

その他の回答 (2 件)

Jan
Jan 2017 年 11 月 28 日
編集済み: Jan 2017 年 11 月 28 日

1 投票

B = [1 2 3 4 5; ...
6 7 8 9 10; ...
11 12 13 14 15];
siz = size(B);
idx = sub2ind(siz, randi([1,3], 1, siz(2)), 1:siz(2));
A = zeros(siz);
A(idx) = B(idx)

5 件のコメント

Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 29 日
thanks for your response but i am getting error in idx = sub2ind(siz, randi([1,3], 1, siz(2)), 1:siz(2)); stating (Expression or statement is incorrect--possibly unbalanced (, {, or [.).how to overcome it.
Andrei Bobrov
Andrei Bobrov 2017 年 11 月 29 日
Prabha!
All work on my desktop:
>> B = [1 2 3 4 5; ...
6 7 8 9 10; ...
11 12 13 14 15];
siz = size(B);
idx = sub2ind(siz, randi([1,3], 1, siz(2)), 1:siz(2));
A = zeros(siz);
A(idx) = B(idx)
A =
1 0 3 0 5
0 0 0 9 0
0 12 0 0 0
>>
Andrei Bobrov
Andrei Bobrov 2017 年 11 月 29 日
Hi Jan!
+1.
Jan
Jan 2017 年 11 月 29 日
編集済み: Jan 2017 年 11 月 29 日
@Prabha: I do not get an error if I run the posted code or the snippet "idx = sub2ind(siz, randi([1,3], 1, siz(2)), 1:siz(2));".
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 29 日
Thanks for your response.Now the code is getting executed.

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

Andrei Bobrov
Andrei Bobrov 2017 年 11 月 28 日
編集済み: Andrei Bobrov 2017 年 11 月 28 日

0 投票

A = B;
s = size(A);
[~,ii] = sort(rand(s));
n = s(1)*(0:s(2)-1);
ii = bsxfun(@plus,ii,s(1)*(0:s(2)-1));
jj = zeros(s);
jj(bszfun(@plus,randi([2,s(1)],1,s(2)),n)) = 1;
ii(cumsum(jj)>0) = 0;
A(ii(ii>0)) = 0;
or
A = B;
s = size(A);
A(rand(s) < .5) = 0;
k = ~any(A);
if any(k)
ii = find(k);
jj = sub2ind(s,randi(s(1),1,numel(ii)),ii);
A(jj) = B(jj);
end

6 件のコメント

Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 28 日
A = B; s = size(A); [~,ii] = sort(rand(s)); n = s(1)*(0:s(2)-1); ii = ii + s(1)*(0:s(2)-1); jj = zeros(s); jj(randi([2,s(1)],1,s(2)) + n) = 1; ii(cumsum(jj)>0) = 0; A(ii(ii>0)) = 0;
If i runthis code i am getting error in this line ii = ii + s(1)*(0:s(2)-1){Error using + Matrix dimensions must agree}.Could you tell me to get rid of it.
Andrei Bobrov
Andrei Bobrov 2017 年 11 月 28 日
編集済み: Andrei Bobrov 2017 年 11 月 28 日
I'm corrected.
Use second variant (after word "or").
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 29 日
If i use the first variant i am unable to get one value in each column with the rest of the other values as zeros.
If i use second variant I am unable to get the result and in the command line L(rand(s) < .5) = 0 what is the pupose of .5 in that command.could you tell me how can i solve it.
Andrei Bobrov
Andrei Bobrov 2017 年 11 月 29 日
Hm! :)
s = size(B);
A = B;
[~,ii] = sort(rand(s));
A(sub2ind(s,ii(1:end-1,:),repmat(1:s(2),s(1)-1,1))) = 0;
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 29 日
thanks for sending the code.the code is getting executed.
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 29 日
how can i plot the graph of A with respect to rows and columns for the final output.

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

カテゴリ

ヘルプ センター および 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