I don't understand why the last line is wrong

2 views (last 30 days)
sz = size(img);
Unrecognized function or variable 'img'.
out_sz = size(gray);
BinEdges = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
r = 377;
%ben trai
for ca=0:197
a = randn(r, ca);
end
figure
q = histogram(a);
%ben phai
for cb = 198:394
b = randn(r, cb);
end
figure
w = histogram(b);
figure
e = abs(q-w);

Answers (2)

Rik
Rik on 30 May 2023
Your syntax is confusing. I don't think this code does what you think it does. The a and B variables are overwritten every iteration, so only the last is used.
The underlying cause is probably that you don't explicitly use the same bins for the two histograms, so the chance they happen to be the same is slim.

Image Analyst
Image Analyst on 30 May 2023
Not sure what your goal is, but you can't subtract histogram objects. Perhaps you want to use histcounts() or get a property of your histogram objects. But you need to make sure the arrays are the same size (histograms have the same numbers of bins). So you might need to send in the 'Edges' array to histogram to make sure of that.
img = imread('cameraman.tif');
sz = size(img);
out_sz = size(gray);
BinEdges = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
r = 377;
%ben trai
for ca=0:197
a = randn(r, ca);
end
figure
q = histogram(a)
q =
Histogram with properties: Data: [377×197 double] Values: [1 1 0 2 1 2 2 8 10 14 14 25 42 53 74 87 131 144 196 206 300 377 453 540 584 735 875 1090 1174 1370 1534 1643 1873 2096 2234 2436 2520 2677 2825 2914 2964 3009 2970 2831 2891 … ] NumBins: 84 BinEdges: [-4.2000 -4.1000 -4 -3.9000 -3.8000 -3.7000 -3.6000 -3.5000 -3.4000 -3.3000 -3.2000 -3.1000 -3 -2.9000 -2.8000 -2.7000 -2.6000 -2.5000 -2.4000 -2.3000 -2.2000 -2.1000 -2 … ] BinWidth: 0.1000 BinLimits: [-4.2000 4.2000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0 0 0] Show all properties
%ben phai
for cb = 198:394
b = randn(r, cb);
end
figure
w = histogram(b)
w =
Histogram with properties: Data: [377×394 double] Values: [3 3 0 4 1 2 2 0 1 2 5 3 7 10 4 7 12 15 23 15 34 34 42 38 36 71 59 60 68 99 130 114 145 161 167 182 202 249 288 308 359 384 404 458 480 520 627 662 738 868 878 929 967 1024 … ] NumBins: 171 BinEdges: [-4.1000 -4.0500 -4.0000 -3.9500 -3.9000 -3.8500 -3.8000 -3.7500 -3.7000 -3.6500 -3.6000 -3.5500 -3.5000 -3.4500 -3.4000 -3.3500 -3.3000 -3.2500 -3.2000 -3.1500 -3.1000 … ] BinWidth: 0.0500 BinLimits: [-4.1000 4.4500] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0 0 0] Show all properties
figure
e = abs(q-w);
Operator '-' is not supported for operands of type 'matlab.graphics.chart.primitive.Histogram'.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!