Concatenating column vector to a cell array - matlab

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

Related

In KDB, how do I sum the previous 3 numbers in a list?

Say I have a list of numbers:
j: (til 40)*9
0 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180 189 198 207 216 225 234 243 252 261 270 279 288 297 306 315 324 333 342 351
What's the most elegant way to get the sum of the previous 3 (or n) numbers in the list? (Ideally considering large RAM constrained lists).
Does this work?
q)3 msum j
0 9 27 54 81 108 135 162 189 216 243 270 297 324 351 378 405 432 459 486 513 ..

Write a table object into csv in matlab

I have a table object in Matlab with cells as shown in the snapshot:
The cells inside Land and Seamark are as below:
The class of the objects are as below:
>> class(FileData.gTruth.LabelData.Land)
ans =
'cell'
>> class(FileData.gTruth.LabelData.Land{1,1})
ans =
'double'
>> class(FileData.gTruth.LabelData)
ans =
'table'
I tried some syntax like writetable and csvwrite but i am not getting the right format of output. The reading of the Land and Seamark as shown in the figure gets jumbled(reading is columnwise and not row-wise).
I want my output to be in this order:
[1063 126 115 86] [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] [562 220 33 51;1736 167 26 28;532 130 18 15;393 129 23 14]
Code so far:
writetable(FileData.gTruth.LabelData,'labelled1.txt','Delimiter' , ';');
You can simply use reshape on the transpose of the two-dimensional matrices to build a new table:
Ship = [1063 126 115 86]
Land = {[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]}
Seamark = {[562 220 33 51; 1736 167 26 28; 532 130 18 15; 393 129 23 14]}
t = table(Ship,Land,Seamark);
t2 = table(t.Ship,reshape(t.Land{:}.',1,[]),reshape(t.Seamark{:}.',1,[]))
writetable(t2,'mycsv.csv','WriteVariableNames',false)
The first and only row of mycsv.csv file is:
1063 126 115 86 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 562 220 33 51 1736 167 26 28 532 130 18 15 393 129 23 14
I used the WriteVariableNames,false Name-Value pair to indicate that the variable names are not to be included in the first row of the file.

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.

How to delete all numbers in a vector which are smaller than any previous number?

I have data set like:
1 14.8759
2 14.083
3 0.735268
4 18.2378
5 17.3748
6 4.07867
7 18.2032
8 15.6929
9 4.03338
10 19.0308
11 17.4139
12 17.4139
13 19.8453
14 4.91288
15 20.6746
16 16.578
17 14.8548
18 23.9831
19 19.0691
20 19.0777
21 3.24368
22 25.6457
23 -5.95598
24 32.3198
25 8.20419
26 22.3266
27 17.4016
28 9.0672
29 24.8722
30 24.8262
31 19.8966
32 34.7338
33 29.8088
34 33.1393
35 28.1402
36 35.6231
37 26.4872
38 3.2392
39 5.73463
40 26.4754
41 33.9667
42 27.3048
43 34.75
44 37.2759
45 15.6929
46 28.9686
47 44.6922
48 37.2799
49 25.699
50 45.4923
51 32.2579
52 25.699
53 29.7885
54 50.4719
55 20.6746
56 30.6061
57 38.0448
58 11.5342
59 52.9365
60 44.7128
61 38.0448
62 44.6621
63 13.1939
64 28.9542
65 46.3637
66 13.1939
67 10.7318
68 31.4318
69 29.7885
70 22.3399
71 29.7885
72 26.4754
73 55.4135
74 48.8326
75 42.2395
76 19.0174
77 7.4035
78 13.1939
79 33.9055
80 14.8935
81 27.3048
82 6.56548
83 64.4474
84 48.7848
85 59.5214
86 31.4915
87 59.5214
88 19.8966
89 57.0318
90 21.5631
91 20.7273
92 66.0889
93 58.6749
94 20.6803
95 52.1244
96 16.5242
97 51.3028
98 10.7037
99 12.3958
100 26.5265
101 30.6061
102 74.2826
103 50.4806
104 12.3958
105 17.354
106 40.5832
107 19.8514
108 63.6089
109 27.3559
110 9.06318
111 11.564
112 39.7561
113 29.8368
114 17.3615
115 19.0241
116 69.3539
117 35.6231
118 38.8777
119 34.7394
120 60.3455
121 25.6969
122 54.5637
123 25.6969
124 79.2023
125 31.4876
126 28.184
127 13.2268
128 34.7394
129 12.3602
130 29.0096
131 47.9604
132 82.4815
133 77.5533
134 14.8935
135 33.9055
136 16.5172
137 41.4113
138 34.7956
139 64.4558
140 29.8368
141 19.0108
142 26.5265
143 36.4452
144 50.4761
145 4.87781
146 83.3041
147 61.9694
148 26.5265
149 1.5427
150 71.8344
151 24.8158
152 94.7328
153 19.8915
154 36.4452
155 32.2504
156 26.5265
157 89.0202
158 29.8347
159 93.9223
160 87.3855
161 4.89738
162 88.1694
163 24.0448
164 51.2987
165 65.2679
166 89.8386
167 33.9055
168 67.7414
169 88.9942
170 19.0174
171 92.2651
172 49.6527
173 18.1971
174 19.0108
175 33.9667
176 92.2611
177 32.2789
178 92.2577
179 4.89738
180 102.898
181 34.7956
182 95.5292
183 28.9542
184 91.451
185 25.6457
186 74.2944
187 25.6516
188 47.1323
189 34.7338
190 94.7081
191 97.9775
192 105.334
193 89.812
194 93.8991
195 88.1756
196 10.7318
197 49.611
198 97.1618
199 2.40369
200 44.7128
201 35.6263
202 42.1795
203 53.7678
204 70.2067
205 28.9542
206 19.0241
207 111.849
208 19.8915
209 95.5218
210 38.8723
211 101.238
212 19.8393
213 92.2651
214 102.053
215 24.8221
216 116.713
217 88.9912
218 88.1756
219 115.102
220 58.6995
221 19.8393
222 27.3171
223 23.1511
224 53.7678
225 99.6138
226 120.79
227 32.2579
228 90.6265
229 38.0448
230 48.8284
231 111.054
232 112.608
233 66.9162
234 100.431
235 63.6317
236 19.8334
237 35.6263
238 17.3615
239 2.39774
240 29.7885
241 71.0225
242 66.9162
243 25.6457
244 128.908
245 12.3602
246 93.8991
247 123.218
248 24.8221
249 33.1393
250 110.194
251 31.4547
252 12.3958
253 92.2611
254 10.7037
255 90.6302
256 96.3458
257 102.053
258 37.2167
259 93.0788
260 19.0108
261 102.063
262 16.5617
263 49.611
264 135.388
265 117.522
266 92.2879
267 118.378
268 116.706
269 24.0448
270 128.941
271 132.182
272 137.009
273 48.7848
274 32.2789
275 137.826
276 137.009
277 117.522
278 54.5904
279 16.5172
280 141.064
281 63.6317
282 27.3559
283 108.587
284 38.8723
285 140.247
286 106.13
287 135.426
288 67.7371
289 19.8915
290 112.652
291 27.3227
292 117.522
and want to ignore/delete any Y value which is smaller than its previous value (and delete its corresponding X too) and put the new data set into a new file so that all resulted Y values would be in increasing order.
Thanks.
Assuming:
data = [1 14.8759
2 14.083
3 0.735268
... ... ];
You could do that:
keep = false(size(data, 1), 1);
largest = -Inf;
for i = 1:size(data, 1)
if data(i,2) > largest
largest = data(i,2);
keep(i) = true;
end
end
newdata = data(keep,:)
Result:
newdata =
1.0000 14.8759
4.0000 18.2378
10.0000 19.0308
13.0000 19.8453
15.0000 20.6746
18.0000 23.9831
22.0000 25.6457
24.0000 32.3198
32.0000 34.7338
36.0000 35.6231
44.0000 37.2759
47.0000 44.6922
50.0000 45.4923
54.0000 50.4719
59.0000 52.9365
73.0000 55.4135
83.0000 64.4474
92.0000 66.0889
102.0000 74.2826
124.0000 79.2023
132.0000 82.4815
146.0000 83.3041
152.0000 94.7328
180.0000 102.8980
192.0000 105.3340
207.0000 111.8490
216.0000 116.7130
226.0000 120.7900
244.0000 128.9080
264.0000 135.3880
272.0000 137.0090
275.0000 137.8260
280.0000 141.0640
If you've lot of data then it will be better to use vectorization. Removing for loops will make it faster.
Let's say 'A' is your second column (data).
A = 5 4 8 8 2 5 5 7 8 8;
Since your first column is just index we can leave it for now (Even if it's not you can copy second column to 'A' and proceed).
B = A - [-inf A(1:end-1)];
Aout = [find(B>=0);A(B>=0)];
If your first column is not just index copy it to say 'C' and change the last line to the following.
Aout = [C(B>=0);A(B>=0)];
Use bsxfun to compare each element with all the others, and from that generate a logical index that selects the desired rows:
result = data(~any(triu(bsxfun(#lt, data(:,2).', data(:,2)))), :);

How can I display a large matrix without the word "Columns" appearing?

I want to display a large matrix, but I don't like the words "Columns x to y" to show. How can I do this?
You can use the function NUM2STR to format a large 2-D matrix A into a character array and display that. For example:
>> A = magic(15); %# This would likely break up columns when displayed
>> num2str(A) %# This won't
ans =
122 139 156 173 190 207 224 1 18 35 52 69 86 103 120
138 155 172 189 206 223 15 17 34 51 68 85 102 119 121
154 171 188 205 222 14 16 33 50 67 84 101 118 135 137
170 187 204 221 13 30 32 49 66 83 100 117 134 136 153
186 203 220 12 29 31 48 65 82 99 116 133 150 152 169
202 219 11 28 45 47 64 81 98 115 132 149 151 168 185
218 10 27 44 46 63 80 97 114 131 148 165 167 184 201
9 26 43 60 62 79 96 113 130 147 164 166 183 200 217
25 42 59 61 78 95 112 129 146 163 180 182 199 216 8
41 58 75 77 94 111 128 145 162 179 181 198 215 7 24
57 74 76 93 110 127 144 161 178 195 197 214 6 23 40
73 90 92 109 126 143 160 177 194 196 213 5 22 39 56
89 91 108 125 142 159 176 193 210 212 4 21 38 55 72
105 107 124 141 158 175 192 209 211 3 20 37 54 71 88
106 123 140 157 174 191 208 225 2 19 36 53 70 87 104