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 Kommentare

Akira Agata
Akira Agata am 28 Nov. 2017
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
Jan
Jan am 28 Nov. 2017
@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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Akira Agata
Akira Agata am 28 Nov. 2017

0 Stimmen

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 Kommentare

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?
Stephen23
Stephen23 am 29 Nov. 2017
@Prabha Kumaresan: please format your code correct so that it is readable. Jan Simon has already explained how you can do this.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jan
Jan am 28 Nov. 2017
Bearbeitet: Jan am 28 Nov. 2017

1 Stimme

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 Kommentare

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
>>
Andrei Bobrov
Andrei Bobrov am 29 Nov. 2017
Hi Jan!
+1.
Jan
Jan am 29 Nov. 2017
Bearbeitet: Jan am 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.

Melden Sie sich an, um zu kommentieren.

Andrei Bobrov
Andrei Bobrov am 28 Nov. 2017
Bearbeitet: Andrei Bobrov am 28 Nov. 2017

0 Stimmen

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 Kommentare

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 am 28 Nov. 2017
Bearbeitet: Andrei Bobrov am 28 Nov. 2017
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by