how to plot a graph in matlab ?

Let given array A with 512*512 elements are (here one sample of 4*4 is given)
5 4 6 8
9 11 30 34
12 13 13 14
how to plot a graph where x-axis contains the values of individual elements of 512*256 matrix and y-axis contain the frequency of the difference value. The frequency of difference value can be obtained as
1 -2
-2 -4
-1 -1
here the difference value can be found by subtracting the 2 consecutive elements in a row.Now suppose the original matrix A is 512*512 elements, then the difference matrix will be 512 * 256 elements.
Kindly suggest

5 Commenti

KSSV
KSSV il 3 Ott 2017
Do you have any pictorial example?
A(:, 1:2:end) - A(:,2:2:end) would give you a 512 x 256 array.
You have not indicated why the output should only be 256 rows.
I m really sorry ...the output should be 512 * 256 only..
ok..by doing A(:, 1:2:end) - A(:,2:2:end) gives 512 x 256 array. Then how can i plot a graph with x-axis as its elements of 512*256 array and y-axis as its frequency of occurence.
@ KSSV SIR, I HAVE ATTACHED A SAMPLE FILE THE PLOT SHOULD COME OR LOOKS LOOKS LIKE THIS.

Accedi per commentare.

 Risposta accettata

B = A(:, 1:2:end) - A(:,2:2:end);
histogram(B, 'binmethods', 'integers')
Note: this might require a fairly recent MATLAB (I do not recall ever having seen that option before.)

9 Commenti

Dear sir, when i use, histogram(B, 'binmethods', 'integers') The error is comming as Undefined function 'histogram' for input arguments of type 'uint8'. Kindy tell what has to be done now.?
Histogram was introduced in R2014b, and has always supported uint8. On the other hand, subtracting uint8 data can never give you negative values, so even if you were using a new enough version then for uint8 data you should use
B = double(A(:, 1:2:end)) - double(A(:,2:2:end));
For versions older than R2014b:
bins = min(B(:)) : max(B(:));
hist(B, bins);
aditya sahu
aditya sahu il 3 Ott 2017
Modificato: aditya sahu il 3 Ott 2017
Dear @ Walter Roberson thank for your help. but the below code is working.But only problem is in x-axis i want to limit it to (-40 to 40) ..can you help to sort this issue.
z=double(int32(A(:, 1:2:end))) - double(int32((A(:,2:2:end))))
[a,b]=hist(z,unique(z));
out=[b' sum((a),2)]
x=out(:,1);
y=out(:,2);
plot(x,y,'--bo')
Walter Roberson
Walter Roberson il 3 Ott 2017
Modificato: Walter Roberson il 3 Ott 2017
z = double(A(:, 1:2:end)) - double(A(:,2:2:end));
bins = -40:40;
N = histc(z, bins);
x = bins;
y = N;
plot(x, y, '--bo')
aditya sahu
aditya sahu il 3 Ott 2017
Modificato: Walter Roberson il 3 Ott 2017
No sir your code is giving incorrect plot ..kindly tell in my code how can i restrict the x-axis range to -40 to 40.The code is
z=double(int32(b(:, 1:2:end))) - double(int32((b(:,2:2:end))));
[a,c]=hist(z,unique(z));
out=[c' sum((a),2)];
x=out(:,1);
y=out(:,2);
plot(x,y, '--bo')
aditya sahu
aditya sahu il 3 Ott 2017
Modificato: Walter Roberson il 3 Ott 2017
ya..i got it...
plot(x(x>-40 & x<40), y(x>-40 & x<40),'or')
..is it right..
why are you converting to int32 before converting to double??
z = double(A(:, 1:2:end)) - double(A(:,2:2:end));
bins = -40:40;
N = histc(z(:), bins);
x = bins;
y = N;
plot(x, y, '--bo')
Thank you dear @ Walter Roberson.. Actually not required,but i have taken two different matrices where one is in uint8 and other is double. thats i have brrought back both to unit32..Thats all only.
It is not a problem to use double() on an array that is already double(), so leave out the conversion to int32()

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by