How to add elements to a matrix one-by-one?

5 vues (au cours des 30 derniers jours)
Elizaveta Grigorashvili
Elizaveta Grigorashvili le 6 Déc 2018
Commenté : Luna le 7 Déc 2018
I have a matrix [0 1; 1 0]. I need to:
1) choose random row from this matrix
2) duplicate every non-zero element from this row with a some probability and add it to the matrix.
3) enlarge the size of the matrix (to keep it square)
4) repeate the procedure with the new matrix (with added row), say, 10 times.
The idea is to use this matrix to construct a graph (where zeros correspond to the absence of linkadge between two nodes and ones - to the presence of linkadge).
Now I stuck on the stage of enlarging the matrix: as every element of the randomly chosen row should be checked alone, matlab do not allow to add elements one-by-one in one cycle.
conmat = [0 1; 1 0] ;
S=size(conmat,1);
r=randi(S);
sigma=0.3;
a=rand;
rndrow=conmat(r,:);
rowadd=[];
for j=1:size(conmat,1);
if sigma>a
rowadd(j)=[rowadd rndrow(j)]
elseif sigma<a
rowadd(j)= [0]
end
conmat=[conmat rowadd] %ERROR
coladd=transpose(conmat(3,:))
coladd=[coladd; 0]
conmat=[conmat coladd]
end
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in network_181129 (line 15)
conmat=[conmat rowadd]
I feel I need to use some different approach not syntaxis tricks but I'm not so experinced in coding and Matlab and already spent a lot of time on this problem. I will appreciate any help and ideas, thank you in advance!

Réponse acceptée

Luna
Luna le 6 Déc 2018
Modifié(e) : Luna le 6 Déc 2018
Hi,
I tried to fix the errors (cut and paste some lines inside the inner for loop), and added outer for loop for 10 times calculation(I assume your random variable a changes in every iteration).
Try this below. It takes a little bit long time since I ended up with conmat 2048x2048 matrix. But maybe there could be a high performanced version of it.
conmat = [0 1; 1 0] ;
for m=1:10
S=size(conmat,1);
r=randi(S);
sigma=0.3;
a=rand;
for j=1:size(conmat,1)
rowadd=zeros(1,size(conmat,1));
rndrow=conmat(r,:);
if sigma > a
rowadd= rndrow;
elseif sigma < a
% rowadd(j)= 0
end
conmat=[conmat; rowadd]; %ERROR
coladd=transpose(conmat(3,:));
coladd=[coladd; 0];
conmat=[conmat coladd];
end
end
If you want to replicate matrices you can use repmat function also. Here the link: repmat
  2 commentaires
Elizaveta Grigorashvili
Elizaveta Grigorashvili le 7 Déc 2018
Modifié(e) : Elizaveta Grigorashvili le 7 Déc 2018
Hi Luna, thank you a lot! It was reaaly helpful. I changed a little the code you've suggested and obtained the needed graph. Now the script looks like that:
conmat = [0 1; 1 0] ;
for m=1:10
S=size(conmat,1);
r=randi(S);
a=rand;
sigma=0.5;
for j=1:size(conmat,1)
rowadd=zeros(1,size(conmat,1));
rndrow=conmat(r,:);
if sigma > a
rowadd(j)= rndrow(j);
rowadd(j+1)=1;
elseif sigma < a
rowadd(j)= rndrow(j);
rowadd(j+1)==0;
end
conmat=[conmat; rowadd];
coladd=transpose(rowadd);
coladd=[coladd; 0];
conmat=[conmat coladd];
conmat( ~any(conmat,2), : ) = [];
conmat( :, ~any(conmat,1) ) = [];
end
end
G=graph(conmat);
plot(G)
Luna
Luna le 7 Déc 2018
Glad to be helpful! :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by