G=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
Code one siz = size(G); idx = sub2ind(siz, randi([1,4], 1, siz(2)), 1:siz(2)); C = zeros(siz); C(idx) = G(idx);
code two s = size(G); C = G; [~,ii] = sort(rand(s)); C(sub2ind(s,ii(1:end-1,:),repmat(1:s(2),s(1)-1,1))) = 0;
code 3 sz = size(G); rowPos = randi(sz(1),sz(2),1); linInd = sub2ind(sz, rowPos, (1:sz(2))'); idx = false(sz); idx(linInd) = true; C = G; C(~idx) = 0;
Could anybody tell me how to plot the graph of code one,code two and code three separately having rows on the x axis and columns on the y axis.

Réponses (1)

Image Analyst
Image Analyst le 29 Nov 2017
Not exactly sure what you mean. Like this: ????
G=[1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16]
% Code one
siz = size(G);
idx = sub2ind(siz, randi([1,4], 1, siz(2)), 1:siz(2));
C = zeros(siz);
C(idx) = G(idx);
subplot(3, 1, 1);
plot(C, 'LineWidth', 2);
grid on;
% code two
s = size(G);
C = G;
[~,ii] = sort(rand(s));
C(sub2ind(s,ii(1:end-1,:),repmat(1:s(2),s(1)-1,1))) = 0;
subplot(3, 1, 2);
plot(C, 'LineWidth', 2);
grid on;
% code 3
sz = size(G);
rowPos = randi(sz(1),sz(2),1);
linInd = sub2ind(sz, rowPos, (1:sz(2))');
idx = false(sz);
idx(linInd) = true;
C = G;
C(~idx) = 0;
subplot(3, 1, 3);
plot(C, 'LineWidth', 2);
grid on;
C is a 4 x 4 array. So do you mean each columns is a new set of y values, so column 1 has y1 values, column2 has y2 values, column 3 has y3 values, and column 4 has y4 values, and the x for all of those is just the row number = [1,2,3,4]?

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by