Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

could anyone help me to execute the following code as i am unable to get the result.

2 Ansichten (letzte 30 Tage)
UE=[2 4 6 8 10];
SC=[20 40 50 60 80];
for t=1:length(UE)
for r=1:length(SC)
G=rand(UE(t),SC(r));
B=zeros(UE(t),SC(r));
N=2;
for g=1:SC(r)
C= B(:,g);
zeroidx = find(~C);
replaceidx = zeroidx(randperm(numel(zeroidx), N-sum(C)));
C(replaceidx) = 1 ;
S(:,g) = [C];
end
Z = G .* S
end
end

Antworten (1)

Ahmos Sansom
Ahmos Sansom am 12 Dez. 2017
Not sure what this code does but you need to pre-allocate matrix S.
i.e.
UE=[2 4 6 8 10];
SC=[20 40 50 60 80];
for t=1:length(UE)
for r=1:length(SC)
G=rand(UE(t),SC(r));
B=zeros(UE(t),SC(r));
S = B;
N=2;
for g=1:SC(r)
C= B(:,g);
zeroidx = find(~C);
replaceidx = zeroidx(randperm(numel(zeroidx), N-sum(C)));
C(replaceidx) = 1 ;
S(:,g) = C;
end
Z = G .* S;
end
end
  2 Kommentare
Prabha Kumaresan
Prabha Kumaresan am 13 Dez. 2017
thanks but in the command window it is getting displayed as matrix dimensions must agree in the command line Z=G.*S
Walter Roberson
Walter Roberson am 13 Dez. 2017
Not when I copy and paste that code. That code executes for me.
I do notice, though, that you overwrite all of Z for each t and r value. If you are going to overwrite it all, why bother calculating it?

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by