Dear members,
I have this program for one matrix
H=[0 1 1 0 1 0 1 1 1 0;
0 1 1 1 0 1 0 0 1 1;
1 1 1 0 0 1 1 1 0 0;
1 0 0 1 1 0 0 1 1 1;
1 0 0 1 1 1 1 0 0 1];
dvi = sum(H,1);
dci = sum(H,2)';
[adv,bdv] = hist(dvi,unique(dvi));
[adc,bdc] = hist(dci,unique(dci));
dv = sum((adv./sum(adv)).*bdv);
dc = sum((adc./sum(adc)).*bdc);
set(gca,'XTick',[]);
set(gca,'YTick',[]);
adv(adv == 0) = NaN;
stem(bdv,adv,'linewidth',2);
hold on;
adc(adc == 0) = NaN;
stem (bdc,adc, 'linewidth',2);
xlim([min(min(bdv),min(bdc))-1 max(max(bdv),max(bdc))+1]);
xlabel('Degree of nodes');
ylabel('Number of nodes');
grid on;
axis on;
legend('dv','dc','location','NorthEast');
I got this figure
In the case of adding another matrix and to plot the result of the two matrices in one figure, how can I do that please.

 Akzeptierte Antwort

Star Strider
Star Strider am 22 Okt. 2021

0 Stimmen

Add the second matrix to the code, concatenate it with ‘H’ along dimension 3, then use a for loop to address and plot them.
H1=[0 1 1 0 1 0 1 1 1 0;
0 1 1 1 0 1 0 0 1 1;
1 1 1 0 0 1 1 1 0 0;
1 0 0 1 1 0 0 1 1 1;
1 0 0 1 1 1 1 0 0 1];
H2 = randi([0 1], size(H1)); % Second Matrix
Hm = cat(3,H1,H2); % Concatenate To 3D Array
hold on
for k = 1:size(Hm,3)
H = Hm(:,:,k); % Set 'H' To Correct Matrix
dvi = sum(H,1);
dci = sum(H,2)';
[adv,bdv] = hist(dvi,unique(dvi));
[adc,bdc] = hist(dci,unique(dci));
dv = sum((adv./sum(adv)).*bdv);
dc = sum((adc./sum(adc)).*bdc);
set(gca,'XTick',[]);
set(gca,'YTick',[]);
adv(adv == 0) = NaN;
stem(bdv,adv,'linewidth',2);
% hold on;
adc(adc == 0) = NaN;
stem (bdc,adc, 'linewidth',2);
xlim([min(min(bdv),min(bdc))-1 max(max(bdv),max(bdc))+1]);
end
hold off
xlabel('Degree of nodes');
ylabel('Number of nodes');
grid on;
axis on;
legend('dv','dc','location','NorthEast');
This works with the matrices, however it does not appear to produce the result of the original code with the random matrix I created here to test the code. Since I am not certain what that is, I defer to you to solve that. It might be necessary to use 'DisplayName' to create the legend correctly.
.

4 Kommentare

high speed
high speed am 22 Okt. 2021
@Star Strider Thank you so much.
Can you tell me please how to create the legend using "DisplayName". Because I want to name dv(blue),dc(red) for (H1) and dv(orange),dc(purple) for (H2)
As always, my pleasure!
It is difficult for me to understand what the posted code does, so this is a separate example that would be possible to adapt with the histogram calls —
red = [1 0 0];
blue = [0 0 1];
orange = [0.8500 0.3250 0.0980];
purple = [0.4940 0.1840 0.5560];
clrs = { blue red; orange purple};
figure
hold on
for k = 1:2
x = [1 2]+2*(k-1);
y = rand(1,2);
bar(x(1), y(1), 0.1, 'FaceColor',clrs{k,1}, 'DisplayName',sprintf('dv H%d',k))
bar(x(2), y(2), 0.1, 'FaceColor',clrs{k,2}, 'DisplayName',sprintf('dc H%d',k))
end
grid
hold off
legend('Location','best')
Thsi should get you started.
.
high speed
high speed am 22 Okt. 2021
@Star Strider Thank you so much. I really appreciate your help
Star Strider
Star Strider am 22 Okt. 2021
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by