subtracting two matrices in matlab, the negative values in result are substituted by zero - matlab

I have two matrices in matlab,
> IRwindow =
>
> **183** 171 150 125 137
138 167 184 173 152
105 114 141 167 185
148 113 105 115 141
186 183 147 112 105
>
> ILwindow =
>
> **201** 170 165 177 203
181 174 167 169 189
154 150 156 168 181
187 175 158 131 144
173 186 183 167 141
I want to subtract these two matrices element-wise and get the result; for example for first element (183 - 201= -18 ) BUT the output for this element gives zero. the outcome result will be as below:
> IRwindow - ILwindow
ans =
**0** 1 0 0 0
0 0 17 4 0
0 0 0 0 4
0 0 0 0 0
13 0 0 0 0
how could I keep the real results? without getting zero for negatives in my result-matrix

Run the following example code:
%# Create random matrices
X = randi(100, 5, 5);
Y = randi(100, 5, 5);
%# Convert to strictly non-negative format
X = uint8(X);
Y = uint8(Y);
%# Perform subtractions
A = X - Y;
%# Convert to double format
X = double(X);
Y = double(Y);
%# Perform subtraction
B = X - Y;
For a given sample run:
A =
0 15 36 0 0
0 0 0 0 3
0 0 0 25 0
13 0 15 0 0
0 49 0 0 14
while:
B =
-8 15 36 -4 -65
0 -47 -45 -11 3
-18 -17 -11 25 -52
13 -53 15 -15 -1
-35 49 -47 -8 14
You will notice that all the negative numbers in A have been replaced by 0, while the negative numbers in B are displayed correctly.
Stated simply: if you use a numerical format that is not able to store negative numbers, then Matlab truncates at 0. The solution is to convert to a format that is able to accomodate "real" numbers (or a close approximation thereof) such as double, or perhaps in your case one of the int formats may be more appropriate, such as int8, int16, int32 or int64.

Another option is to use single or double on the subtraction in one line as follows:
ans=double(IRwindow-ILwindow)

I dont get the same problem as you: I have this code:
IRwindow = [
183 171 150 125 137
138 167 184 173 152
105 114 141 167 185
148 113 105 115 141
186 183 147 112 105]
ILwindow = [
201 170 165 177 203
181 174 167 169 189
154 150 156 168 181
187 175 158 131 144
173 186 183 167 141]
IRwindow - ILwindow
and i get this output:
IRwindow =
183 171 150 125 137
138 167 184 173 152
105 114 141 167 185
148 113 105 115 141
186 183 147 112 105
ILwindow =
201 170 165 177 203
181 174 167 169 189
154 150 156 168 181
187 175 158 131 144
173 186 183 167 141
ans =
-18 1 -15 -52 -66
-43 -7 17 4 -37
-49 -36 -15 -1 4
-39 -62 -53 -16 -3
13 -3 -36 -55 -36
Check that you are creating your matrices are being created properly (as doubles and not as unsigned integers).

Related

Concatenating column vector to a cell array

I have a matrix in Matlab as below:
a =
1 169 158 147
1 104 165 66
728 105 276 43
950 113 971 40
1 107 810 23
227 133 48 15
618 131 107 20
class(a)
ans =
'double'
I want to add a column to this matrix. When I try this command, i get a wrong result:
A={1;2;3;4;5;6;7}
vertcat(A,a)
Answer is like below:
What I wanted was:
1 1 169 158 147
1 1 104 165 66
1 728 105 276 43
1 950 113 971 40
1 1 107 810 23
1 227 133 48 15
1 618 131 107 20
What mistake I am making and how to I fix it?
Thanks
P.S: I am new to Matlab

Why am I getting the error "Index exceeds matrix dimensions"?

I am currently new to MATLAB. My code is below. I just have a question regarding why I keep getting the error "Index exceeds matrix dimensions" for the functions provided:
a = [105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151 ...
175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167 ...
171 163 87 176 121 120 181 160 194 184 165 145 160 150 181 168 158 208 ...
133 135 172 171 237 170 180 167 176 158 156 229 158 148 150 118 143 141 ...
110 133 123 146 169 158 135 149];
mean = mean(a)
std = std(a)
max = max(a)
min = min(a)
range = range(a)
Don't give variables the same names as existing functions. This shadows the function. When you then try to call the function with an argument you instead end up indexing the variable with the argument, which in this case tries to index elements in the variable that don't exist, hence your error.
Use clear to remove the existing variables, then rerun the calculations with new variable names:
clear mean std max min range;
meanResult = mean(a);
stdResult = std(a);
...
Use clc (clear command window), clear (removes all variables from the workspace) and close all (closes off any previously used figures) to clean you work space. This could help run the script better.
clc, clear, close all
a = [105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151,...,
175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167,...,
171 163 87 176 121 120 181 160 194 184 165 145 160 150 181 168 158 208,...,
133 135 172 171 237 170 180 167 176 158 156 229 158 148 150 118 143 141,...,
110 133 123 146 169 158 135 149];
Mean = mean(a)
Std = std(a)
Max = max(a)
Min = min(a)
Range = range(a)

Make histogram of pixel intensities without imhist

I have used the unique command to get the unique pixel intensities from my image. Then I tried to make a histogram using them, but it doesn't use all of the intensity values
I = imread('pout.tif');
[rows, columns] = size(I);
UniquePixels=unique(I);
hist=histogram(UniquePixels)
An alternative approach would be to use accumarray combined with unique. I would specifically use the third output of unique to transform your data into a consecutive sequence of 1 up to N where N is the total number of unique intensities, then leverage the first output of unique that will give you the list of unique intensities. Therefore, if the first output of unique is A and the output of accumarray is B, the effect is that at location B(i), this gives the total number of intensities of A(i).
Therefore:
[UniquePixels, ~, id] = unique(I);
histo = accumarray(id, 1);
UniquePixels gives you all unique pixels while histo gives you the counts of each unique pixel corresponding to each element in UniquePixels.
Here's a quick example:
>> I = randi(255, 10, 10)
I =
42 115 28 111 218 107 199 60 140 237
203 22 246 233 159 13 100 91 76 198
80 59 2 47 90 231 62 210 190 125
135 233 198 68 131 241 103 4 49 112
43 39 209 38 103 126 25 11 176 114
154 211 222 35 20 125 34 44 47 79
68 138 22 222 62 87 241 166 94 130
167 255 102 148 32 230 244 187 160 131
176 20 67 141 47 95 147 166 199 209
191 113 205 37 62 29 16 115 21 203
>> [UniquePixels, ~, id] = unique(I);
>> histo = accumarray(id, 1);
>> [UniquePixels histo]
ans =
2 1
4 1
11 1
13 1
16 1
20 2
21 1
22 2
25 1
28 1
29 1
32 1
34 1
35 1
37 1
38 1
39 1
42 1
43 1
44 1
47 3
49 1
59 1
60 1
62 3
67 1
68 2
76 1
79 1
80 1
87 1
90 1
91 1
94 1
95 1
100 1
102 1
103 2
107 1
111 1
112 1
113 1
114 1
115 2
125 2
126 1
130 1
131 2
135 1
138 1
140 1
141 1
147 1
148 1
154 1
159 1
160 1
166 2
167 1
176 2
187 1
190 1
191 1
198 2
199 2
203 2
205 1
209 2
210 1
211 1
218 1
222 2
230 1
231 1
233 2
237 1
241 2
244 1
246 1
255 1
If you double check the input example and the final output, you will see that only the unique pixels are shown combined with their counts. Any bins that were zero in count are not shown.

changing the range / limits on a polar chart in octave / matlab

I'm using Octave 4.0 using Linux which is similar to Matlab
Is it possible to have a different range of numbers on a polar chart and have them show up along with the degrees also?
The normal polar plot goes from 0-359 degrees shown in black in image, I would like the range and tick values to be from 0 to 100 shown in red in the image is this possible? If so can two ranges be shown on a polar plot at the same time (0-359 and 0-100) almost like plotting 2 y axis using plotyy on the same plot?
See image below of polar plot
Here's the the numbers 0-359 and there corresponding numbers 0-100 matching up.
0 0
1 0.27855
2 0.5571
3 0.83565
4 1.11421
5 1.39276
6 1.67131
7 1.94986
8 2.22841
9 2.50696
10 2.78552
11 3.06407
12 3.34262
13 3.62117
14 3.89972
15 4.17827
16 4.45682
17 4.73538
18 5.01393
19 5.29248
20 5.57103
21 5.84958
22 6.12813
23 6.40669
24 6.68524
25 6.96379
26 7.24234
27 7.52089
28 7.79944
29 8.07799
30 8.35655
31 8.6351
32 8.91365
33 9.1922
34 9.47075
35 9.7493
36 10.0279
37 10.3064
38 10.585
39 10.8635
40 11.1421
41 11.4206
42 11.6992
43 11.9777
44 12.2563
45 12.5348
46 12.8134
47 13.0919
48 13.3705
49 13.649
50 13.9276
51 14.2061
52 14.4847
53 14.7632
54 15.0418
55 15.3203
56 15.5989
57 15.8774
58 16.156
59 16.4345
60 16.7131
61 16.9916
62 17.2702
63 17.5487
64 17.8273
65 18.1058
66 18.3844
67 18.663
68 18.9415
69 19.2201
70 19.4986
71 19.7772
72 20.0557
73 20.3343
74 20.6128
75 20.8914
76 21.1699
77 21.4485
78 21.727
79 22.0056
80 22.2841
81 22.5627
82 22.8412
83 23.1198
84 23.3983
85 23.6769
86 23.9554
87 24.234
88 24.5125
89 24.7911
90 25.0696
91 25.3482
92 25.6267
93 25.9053
94 26.1838
95 26.4624
96 26.7409
97 27.0195
98 27.2981
99 27.5766
100 27.8552
101 28.1337
102 28.4123
103 28.6908
104 28.9694
105 29.2479
106 29.5265
107 29.805
108 30.0836
109 30.3621
110 30.6407
111 30.9192
112 31.1978
113 31.4763
114 31.7549
115 32.0334
116 32.312
117 32.5905
118 32.8691
119 33.1476
120 33.4262
121 33.7047
122 33.9833
123 34.2618
124 34.5404
125 34.8189
126 35.0975
127 35.376
128 35.6546
129 35.9331
130 36.2117
131 36.4903
132 36.7688
133 37.0474
134 37.3259
135 37.6045
136 37.883
137 38.1616
138 38.4401
139 38.7187
140 38.9972
141 39.2758
142 39.5543
143 39.8329
144 40.1114
145 40.39
146 40.6685
147 40.9471
148 41.2256
149 41.5042
150 41.7827
151 42.0613
152 42.3398
153 42.6184
154 42.8969
155 43.1755
156 43.454
157 43.7326
158 44.0111
159 44.2897
160 44.5682
161 44.8468
162 45.1253
163 45.4039
164 45.6825
165 45.961
166 46.2396
167 46.5181
168 46.7967
169 47.0752
170 47.3538
171 47.6323
172 47.9109
173 48.1894
174 48.468
175 48.7465
176 49.0251
177 49.3036
178 49.5822
179 49.8607
180 50.1393
181 50.4178
182 50.6964
183 50.9749
184 51.2535
185 51.532
186 51.8106
187 52.0891
188 52.3677
189 52.6462
190 52.9248
191 53.2033
192 53.4819
193 53.7604
194 54.039
195 54.3175
196 54.5961
197 54.8747
198 55.1532
199 55.4318
200 55.7103
201 55.9889
202 56.2674
203 56.546
204 56.8245
205 57.1031
206 57.3816
207 57.6602
208 57.9387
209 58.2173
210 58.4958
211 58.7744
212 59.0529
213 59.3315
214 59.61
215 59.8886
216 60.1671
217 60.4457
218 60.7242
219 61.0028
220 61.2813
221 61.5599
222 61.8384
223 62.117
224 62.3955
225 62.6741
226 62.9526
227 63.2312
228 63.5097
229 63.7883
230 64.0669
231 64.3454
232 64.624
233 64.9025
234 65.1811
235 65.4596
236 65.7382
237 66.0167
238 66.2953
239 66.5738
240 66.8524
241 67.1309
242 67.4095
243 67.688
244 67.9666
245 68.2451
246 68.5237
247 68.8022
248 69.0808
249 69.3593
250 69.6379
251 69.9164
252 70.195
253 70.4735
254 70.7521
255 71.0306
256 71.3092
257 71.5877
258 71.8663
259 72.1448
260 72.4234
261 72.7019
262 72.9805
263 73.2591
264 73.5376
265 73.8162
266 74.0947
267 74.3733
268 74.6518
269 74.9304
270 75.2089
271 75.4875
272 75.766
273 76.0446
274 76.3231
275 76.6017
276 76.8802
277 77.1588
278 77.4373
279 77.7159
280 77.9944
281 78.273
282 78.5515
283 78.8301
284 79.1086
285 79.3872
286 79.6657
287 79.9443
288 80.2228
289 80.5014
290 80.7799
291 81.0585
292 81.337
293 81.6156
294 81.8942
295 82.1727
296 82.4513
297 82.7298
298 83.0084
299 83.2869
300 83.5655
301 83.844
302 84.1226
303 84.4011
304 84.6797
305 84.9582
306 85.2368
307 85.5153
308 85.7939
309 86.0724
310 86.351
311 86.6295
312 86.9081
313 87.1866
314 87.4652
315 87.7437
316 88.0223
317 88.3008
318 88.5794
319 88.8579
320 89.1365
321 89.415
322 89.6936
323 89.9721
324 90.2507
325 90.5292
326 90.8078
327 91.0864
328 91.3649
329 91.6435
330 91.922
331 92.2006
332 92.4791
333 92.7577
334 93.0362
335 93.3148
336 93.5933
337 93.8719
338 94.1504
339 94.429
340 94.7075
341 94.9861
342 95.2646
343 95.5432
344 95.8217
345 96.1003
346 96.3788
347 96.6574
348 96.9359
349 97.2145
350 97.493
351 97.7716
352 98.0501
353 98.3287
354 98.6072
355 98.8858
356 99.1643
357 99.4429
358 99.7214
359 100
Here's an image of the numbers 0-359 and there corresponding numbers 0-100 matching up.
Numbers matching up
The polar plot object in Octave adds the rtick and ttick properties to the parent axes which allows you to change the location of the ticks, however, there is unfortunately no tticklabel property that we can use to easily find and modify the theta tick marks.
Instead, we can plot your plot that you want the theta range to be 0 - 100 by first transforming your data to instead be 0 - 2*pi (as is expected by polar). Then after plotting both, we can use findall to locate all text objects, figure out which ones are the theta ticks, create a copy of them and modify one of the sets to appear to be 0 - 100.
% Your first plot is going to use the 0 - 2pi range for theta
theta1 = linspace(0, 2*pi, 1000);
rho1 = sin(theta1 * 5);
plot1 = polar(theta1, rho1);
hold on
% For your second plot, just transform your 0 - 100 range to be 0 - 360 instead
theta2 = 0:100;
rho2 = linspace(0, 1, numel(theta2));
modtheta2 = 2*pi * (theta2 ./ 100);
plot2 = polar(modtheta2, rho2);
% Now we need to modify all of the labels
% Find all of the original labels
labels = findall(gca, 'type', 'text');
% Figure out which ones are the radial labels. To do this we compute the distance
% from the center of the plot and find the most common distance
distances = cellfun(#(x)norm(x(1:2)), get(labels, 'Position'));
% Figure out the most common
[~, ~, b] = unique(round(distances * 100));
h = hist(b, 1:max(b));
labels = labels(b == find(h == max(h)));
% Make a copy of these labels (have to use arrayfun for 4.0.x compatibility)
blacklabels = arrayfun(#(L)copyobj(L, gca), labels);
% Shift these labels outward by 15%
arrayfun(#(x)set(x, 'Position', get(x, 'Position') * 1.15), blacklabels);
% Now set the other labels to red and change their values
set(labels, 'COlor', 'red')
for k = 1:numel(labels)
value = str2num(get(labels(k), 'String'))
% Convert the value to be between 0 and 100
newvalue = 100 * (value / 360);
set(labels(k), 'String', sprintf('%0.2f', newvalue))
end

Matlab find zero value with certain range

I have this matrix:
A =[22 22 142 142 142 92 92 92 0 0
0 109 109 151 151 151 23 23 149 149
0 0 0 152 152 152 38 38 0 0
0 13 13 113 113 113 119 119 119 0
0 8 8 8 84 84 14 14 14 0
0 0 144 144 144 0 0 0 66 66
139 139 139 34 34 34 0 0 0 0
0 0 64 64 64 128 128 59 59 59
83 83 83 65 65 65 67 67 67 0];
How can I find indices (row, column) from matrix with zero value respectively 2 or more?
You can use find as follows:
[r,c] = find(A==0)
[rows,cols] = ind2sub(size(A),find(A==0))
find gives you the indices and ind2sub converts them in column-row format.