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 comentarios

Akira Agata
Akira Agata el 28 de Nov. de 2017
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 el 28 de Nov. de 2017
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 el 28 de Nov. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Akira Agata
Akira Agata el 28 de Nov. de 2017
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 comentarios

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

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 28 de Nov. de 2017
Editada: Jan el 28 de Nov. de 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 comentarios

Prabha Kumaresan
Prabha Kumaresan el 29 de Nov. de 2017
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 el 29 de Nov. de 2017
Hi Jan!
+1.
Jan
Jan el 29 de Nov. de 2017
Editada: Jan el 29 de Nov. de 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));".
Prabha Kumaresan
Prabha Kumaresan el 29 de Nov. de 2017
Thanks for your response.Now the code is getting executed.

Iniciar sesión para comentar.

Andrei Bobrov
Andrei Bobrov el 28 de Nov. de 2017
Editada: Andrei Bobrov el 28 de Nov. de 2017
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 comentarios

Prabha Kumaresan
Prabha Kumaresan el 28 de Nov. de 2017
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 el 28 de Nov. de 2017
Editada: Andrei Bobrov el 28 de Nov. de 2017
I'm corrected.
Use second variant (after word "or").
Prabha Kumaresan
Prabha Kumaresan el 29 de Nov. de 2017
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;
Prabha Kumaresan
Prabha Kumaresan el 29 de Nov. de 2017
thanks for sending the code.the code is getting executed.
Prabha Kumaresan
Prabha Kumaresan el 29 de Nov. de 2017
how can i plot the graph of A with respect to rows and columns for the final output.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Nov. de 2017

Comentada:

el 29 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by