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) ?
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

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 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

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 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));".
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

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").
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2017년 11월 28일

댓글:

2017년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by