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 Comments

What is the rule to determine whether keeping the number (e.g 1 -> 1) or changing to zero (e.g 2 -> 0) ?
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
@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.

Sign in to comment.

 Accepted Answer

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 Comments

but i want to have only one number in each column rest of the other place where the number holds should be 0.
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
thanks for your code.
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?
@Prabha Kumaresan: please format your code correct so that it is readable. Jan Simon has already explained how you can do this.

Sign in to comment.

More Answers (2)

Jan
Jan on 28 Nov 2017
Edited: Jan on 28 Nov 2017
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 Comments

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.
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
>>
Jan
Jan on 29 Nov 2017
Edited: Jan on 29 Nov 2017
@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));".
Thanks for your response.Now the code is getting executed.

Sign in to comment.

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 Comments

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.
I'm corrected.
Use second variant (after word "or").
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.
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;
thanks for sending the code.the code is getting executed.
how can i plot the graph of A with respect to rows and columns for the final output.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!