Why does MATLAB piecewise cubic interpolation give diagonalized plot data in 2-D surface plot fits? - matlab

Does anyone know why does MATLAB 'piecewise cubic interpolation' gives diagonalized plot data in a 2-D surface plot fit? Attached is a 2-D surface plot (below the code & data) from the code using this cubic fit. As can be seen, peaks in the image/plot are diagonalized at five isolated data points (pointing upper left to lower right). Instead of being a symmetric Gaussian spread that was expected. Why does the cubic interpolation do this?
The cubic fit is shown at: http://www.mathworks.co.uk/help/curvefit/fit.html
Below is the matlab code with 'piecewise cubic interpolation' to fit data from:
'helm2Coils126.txt' & 'positionsData2_20x20_2.txt'.
Any help with this would much appreciated.
Thanking you in advance,
Brendan Darrer
================================CODE=====================================
% 2D MIT surface plots of p.d. phase-difference against x y coordinates
% Written by Brendan Darrer
% Date 20th September 2013.
% oscillator = 2.6V at f = 500Hz, lock-in amplifier: sensitivity = 50mV, time constant = 500ms.
% background phase: ---- degrees.
A = load('helm2Coils126.txt')
B = load('positionsData2_20x20_2.txt')
% correcting phase offset
for i=1:10 % columns
for j=1:40 % rows
if (A(j,i) < 0) % correct offset, if e.g. phase = -179 when it should be 181.
A(j,i) = 360 + A(j,i);
end
end
end
p = A';
A2 = p(:)'
A = A2'
B = [B A]
x=B(:,1); y=B(:,2); z=B(:,3);
Fig2Handle = figure('Position', [100, 100, 1049, 895]);
xlin=linspace(min(x),max(x),100); % was 50
ylin=linspace(min(y),max(y),100); % was 50
[X,Y]=meshgrid(xlin,ylin);
% cubic piecewise interpolation:
Z=griddata(x,y,z,X,Y,'cubic');
surf(X,Y,Z) % interpolated
axis tight; hold on
view(0,90);
plot3(x,y,z,'.','MarkerSize',10)
colormap hsv
colorbar
xlabel('x axis / mm')
ylabel('y axis / mm')
zlabel('phase \Delta\phi / degrees')
====================== END OF CODE =============================
%====== helm2Coils126.txt ==== copy & paste into .txt file ========
%====================(10 COLUMNS, 40 ROWS)=====================
-14.690 -144.460 -173.610 -177.820 -179.260 -179.930 179.690 179.580 179.340 179.360
-128.020 -175.360 -177.990 -179.420 179.680 179.420 179.330 179.170 179.160 179.120
-175.050 -178.450 -179.890 179.770 179.350 179.140 179.070 179.070 178.990 178.960
-178.060 -179.590 179.550 179.290 179.070 179.040 178.940 178.870 178.890 178.900
-179.270 179.780 179.360 179.130 178.990 178.910 178.880 178.940 178.730 178.470
-178.900 179.730 179.160 179.000 179.000 178.860 178.900 179.080 178.760 179.450
179.430 -179.870 179.350 179.220 178.910 178.980 178.950 178.990 178.870 178.520
-179.930 179.220 179.040 179.070 178.940 178.840 178.840 178.810 178.810 178.880
179.430 179.130 179.000 179.000 178.850 178.840 178.820 178.800 178.860 178.840
179.370 179.070 178.930 178.880 178.860 178.810 178.880 178.830 178.810 178.790
179.320 179.120 179.000 178.900 178.860 178.840 178.830 178.870 178.860 178.800
179.360 179.140 178.990 178.920 178.880 178.840 178.890 178.860 178.840 178.900
179.470 179.170 178.950 178.960 178.950 178.860 178.840 178.850 178.860 178.830
179.700 179.190 179.060 -2.820 178.860 178.910 178.850 178.840 178.790 178.840
179.870 179.440 179.250 179.020 179.000 178.970 178.870 178.850 178.820 178.850
-179.450 179.690 179.290 179.030 178.920 178.900 178.890 178.870 178.840 178.850
-177.830 -179.580 179.600 179.270 179.090 178.980 178.950 178.880 178.860 178.890
-175.210 -178.450 -179.850 179.560 179.200 179.080 178.960 178.990 178.930 178.910
-129.050 -175.360 -178.460 -179.730 179.650 179.370 179.250 179.120 179.100 179.040
-16.750 -121.430 -175.270 -178.000 -179.460 179.230 179.330 179.400 179.580 -179.960
179.260 179.350 179.520 179.600 179.900 -179.300 -177.900 -174.010 -107.820 -15.650
179.050 179.090 179.140 179.240 179.440 179.650 -179.680 -174.620 -175.350 -107.820
178.920 178.910 178.940 179.050 179.140 179.260 179.550 -179.970 -178.710 -174.930
178.810 178.870 178.870 178.930 178.990 179.040 179.180 179.470 -179.810 -178.250
178.740 178.850 178.840 178.870 178.910 179.030 179.130 179.380 179.840 -178.840
178.460 178.720 178.760 178.760 178.820 178.870 179.040 178.980 179.430 179.750
178.730 178.770 178.790 178.790 178.840 178.810 178.920 179.060 179.220 179.620
178.780 178.840 178.810 178.780 178.820 178.810 178.870 178.950 179.170 179.360
178.910 178.820 178.910 -2.710 178.830 178.830 178.870 178.900 -2.900 179.240
178.840 178.840 178.870 178.840 178.820 178.840 178.810 178.920 179.020 179.300
178.870 178.870 178.870 178.800 178.800 178.700 178.760 178.840 179.040 179.300
178.890 178.840 178.790 178.690 178.480 177.550 178.480 178.860 179.080 179.250
178.740 178.840 178.820 178.760 177.590 -93.940 177.420 178.890 179.120 179.340
178.970 178.840 178.770 178.740 178.520 177.530 178.700 178.990 179.090 179.570
178.830 178.800 178.800 178.760 178.760 178.780 178.830 179.000 179.290 179.810
178.820 178.900 178.840 178.940 178.940 178.920 178.960 179.130 179.410 -179.960
178.970 178.990 178.900 178.960 178.910 179.040 179.190 179.540 -179.930 -178.180
-2.790 179.060 179.040 179.050 179.110 179.320 179.710 -179.720 -178.270 -174.540
179.000 179.140 179.210 179.440 179.410 179.750 -179.470 -177.830 -167.850 -20.630
179.220 179.290 179.400 179.780 179.900 -179.470 -178.300 -171.660 -168.110 -22.730
=========================end 1st text file============================
======= positionsData2_20x20_2.txt ======== copy & paste into .txt file =========
===================(2 COLUMNS, 400 ROWS)==============================
0 247
13 247
26 247
39 247
52 247
65 247
78 247
91 247
104 247
117 247
0 234
13 234
26 234
39 234
52 234
65 234
78 234
91 234
104 234
117 234
0 221
13 221
26 221
39 221
52 221
65 221
78 221
91 221
104 221
117 221
0 208
13 208
26 208
39 208
52 208
65 208
78 208
91 208
104 208
117 208
0 195
13 195
26 195
39 195
52 195
65 195
78 195
91 195
104 195
117 195
0 182
13 182
26 182
39 182
52 182
65 182
78 182
91 182
104 182
117 182
0 169
13 169
26 169
39 169
52 169
65 169
78 169
91 169
104 169
117 169
0 156
13 156
26 156
39 156
52 156
65 156
78 156
91 156
104 156
117 156
0 143
13 143
26 143
39 143
52 143
65 143
78 143
91 143
104 143
117 143
0 130
13 130
26 130
39 130
52 130
65 130
78 130
91 130
104 130
117 130
0 117
13 117
26 117
39 117
52 117
65 117
78 117
91 117
104 117
117 117
0 104
13 104
26 104
39 104
52 104
65 104
78 104
91 104
104 104
117 104
0 91
13 91
26 91
39 91
52 91
65 91
78 91
91 91
104 91
117 91
0 78
13 78
26 78
39 78
52 78
65 78
78 78
91 78
104 78
117 78
0 65
13 65
26 65
39 65
52 65
65 65
78 65
91 65
104 65
117 65
0 52
13 52
26 52
39 52
52 52
65 52
78 52
91 52
104 52
117 52
0 39
13 39
26 39
39 39
52 39
65 39
78 39
91 39
104 39
117 39
0 26
13 26
26 26
39 26
52 26
65 26
78 26
91 26
104 26
117 26
0 13
13 13
26 13
39 13
52 13
65 13
78 13
91 13
104 13
117 13
0 0
13 0
26 0
39 0
52 0
65 0
78 0
91 0
104 0
117 0
130 247
143 247
156 247
169 247
182 247
195 247
208 247
221 247
234 247
247 247
130 234
143 234
156 234
169 234
182 234
195 234
208 234
221 234
234 234
247 234
130 221
143 221
156 221
169 221
182 221
195 221
208 221
221 221
234 221
247 221
130 208
143 208
156 208
169 208
182 208
195 208
208 208
221 208
234 208
247 208
130 195
143 195
156 195
169 195
182 195
195 195
208 195
221 195
234 195
247 195
130 182
143 182
156 182
169 182
182 182
195 182
208 182
221 182
234 182
247 182
130 169
143 169
156 169
169 169
182 169
195 169
208 169
221 169
234 169
247 169
130 156
143 156
156 156
169 156
182 156
195 156
208 156
221 156
234 156
247 156
130 143
143 143
156 143
169 143
182 143
195 143
208 143
221 143
234 143
247 143
130 130
143 130
156 130
169 130
182 130
195 130
208 130
221 130
234 130
247 130
130 117
143 117
156 117
169 117
182 117
195 117
208 117
221 117
234 117
247 117
130 104
143 104
156 104
169 104
182 104
195 104
208 104
221 104
234 104
247 104
130 91
143 91
156 91
169 91
182 91
195 91
208 91
221 91
234 91
247 91
130 78
143 78
156 78
169 78
182 78
195 78
208 78
221 78
234 78
247 78
130 65
143 65
156 65
169 65
182 65
195 65
208 65
221 65
234 65
247 65
130 52
143 52
156 52
169 52
182 52
195 52
208 52
221 52
234 52
247 52
130 39
143 39
156 39
169 39
182 39
195 39
208 39
221 39
234 39
247 39
130 26
143 26
156 26
169 26
182 26
195 26
208 26
221 26
234 26
247 26
130 13
143 13
156 13
169 13
182 13
195 13
208 13
221 13
234 13
247 13
130 0
143 0
156 0
169 0
182 0
195 0
208 0
221 0
234 0
247 0
===================end 2nd text file==============================

Are you sure of the plot you are showing? Below is the result obtained by your code on your data. I'm using Matlab 2012a.

The cubic piecewise interpolation of the data produces a 2D surface plot (as shown in the original question), in MATLAB 2012b and MATLAB 2013a. However I found other Interpolations for scattered data at matlab Interpolations, that include: 'linear' --> Linear interpolation; 'cubic' --> Cubic piecewise interpolation; 'natural' --> Natural neighbour interpolation; 'nearest' --> Nearest neighbour interpolation.
'Natural neighbour' gave the closest to smoothed Gaussian like edges of the data in x and y. However I have stayed with the cubic interpolation, as it gave the a rounded curve in the z component of the image. Whereas, 'natural neighbour' interpolation gives sharp spikes in the z component, which was unrealistic for my data.
Below are 2 surface plots showing the difference between 'natural neighbour' and 'cubic piecewise' interpolation, for an example image of a copper disk in a metallic container, taken from eddy current imaging.

Related

MATLAB Serial incorrectly sending values

I am trying to communicate with a StepRocker motor controller/drive using MATLAB. I have figured out how to format their commands and have been able to get it to move to many of commands. Their commands require that the values be sent in a binary format over RS-232. I started running into some issues and started using an Arduino to echo my commands.
I am currently sending the values 1 - 255 just to test sending bytes over the serial channel. I am sending it to an Arduino and reading the bytes back out of the Arduino. All numbers work except for the numbers between 128 and 159. This is problematic since some of my commands require values in this range.
MATLAB Script:
obj = instrfind; delete(obj); % cleanup any lost objects
clear; close force all; clc; % clear the workspace
sM = serial('COM2','BaudRate',9600, 'Terminator', 'CR', 'Timeout', 10);
fopen(sM);
pause(2); % give port time to open
% make list of numbers 1-255, skip 10/13 since they terminate on the
% arduino
cmd = [1:9 11:12 14:255];
% Send command and get echo
fprintf(sM,cmd);
pause(0.5); % give time for echo
echo = fscanf(sM);
uint8(echo)
fclose(sM);
After executing this script I get the following output:
ans =
Columns 1 through 22
1 2 3 4 5 6 7 8 9 11 12 14 15 16 17 18 19 20 21 22 23 24
Columns 23 through 44
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
Columns 45 through 66
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
Columns 67 through 88
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
Columns 89 through 110
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
Columns 111 through 132
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 63 63 63 63 63 63 63
Columns 133 through 154
63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
Columns 155 through 176
63 63 63 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
Columns 177 through 198
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
Columns 199 through 220
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
Columns 221 through 242
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
Columns 243 through 254
245 246 247 248 249 250 251 252 253 254 255 13
Looking at the output, you can see that the numbers between 128 and 159 all come through as 63. I am at a loss for what is causing this issue. This is very repeatable, and the problem occurs anytime there is a number in this range, no matter its position in the string, no matter the length of the serial string.
Arduino Code:
bool stringComplete = false;
String inputString = "";
void setup() {
Serial.begin(9600);
}
void loop() {
if (stringComplete) {
Serial.print(inputString);
stringComplete = false;
inputString = "";
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\r') {
stringComplete = true;
}
}
}
Any suggestions or ideas on how to solve this problem?

Matlab Spearman Correlation PVAL = 0?

I am conducting Spearman's Correlation with two data sets with 300 objects. These are my variables and commands:
a = [1:300]
b = [1 2 5 11 9 7 24 10 31 23 3 40 6 17 14 20 16 12 33 46 70 37 87 43 98 26 59 58 77 100 35 42 78 80 243 36 33327 4 83 160 163 198 86 94 406 111 28 29 55 113 239 295 110 196 177 32679 229 342 305 300 254 96 210 514 167 172 232 190 117 32081 25 158 19333 241 82 149 159 66 178 24487 68 30 1016 725 266 391 638 348 320 681 242 319 228 381 408 442 202 369 471 821 191 426 8 270 211 2266 619 576 441 680 3431 1167 723 74 318 556 640 395 1059 579 614 212 325 437 323 687 373 599 26637 985 54 84 802 724 154 417 240 1120 818 2309 462 109 104 509 494 427 57 2475 549 396 419 123 580 79 225 1132 351 76 16859 596 862 315 470 992 257 120 409 751 832 285 1534 714 1665 1376 2129 678 416 721 209 31971 183 356 1346 1015 1003 188 1076 1634 608 1056 338 308 145 418 625 1313 121 2484 996 783 329 1185 697 157 1100 175 622 235 456 277 166 2700 1439 461 653 433 540 1191 234 774 1894 1004 741 1062 948 48 99 405 797 237 1104 2286 22620 1429 30672 1808 169 458 22 1115 10660 872 474 1063 88 1727 1017 1107 1398 1519 703 1092 1027 272 263 1152 1770 1099 507 385 2118 19356 1778 2458 410 2110 7522 17166 4065 15136 13294 10876 17174 2434 9898 5663 13594 10506 11552 15635 9322 3223 8949 12388 13216 13851 13852 6696 12177 4700 17199 2067 11110 15486 5664 6593 4701 527 8616 268]
[RHO,PVAL] = corr(b',a','Type', 'Spearman')
RHO =
0.6954
PVAL =
0
Out of the 5 comparisons I made with other data sets of 300 objects, only 1 returned significant P-values. Is there an explanation for this?
I tried a different data set and got a value that was not significant (PVAL > 0.05). I also displayed the answer in a long (15 digits) and exponential form and got 0.00000000000000e+000 using:
format longEng
I also checked with another statistics program that reported the p-value as < 0.0001. This means that the p-value is just really, really small.

Saving (in a matrix) the elapsed time and number of iterations for a large number of cases

I have a program that outputs the number of iterations and a test value, given inputs A1,A2,A3,A4.
I want to run through 5 values each of A1, A2, A3, A4, thus making 625 runs. In the process, I want to save the time elapsed for each run, the number of iterations, and test value in 3 separate matrices.
I have tried using 4 nested for loops, and made progress, but need some help on indexing the elements of the matrices. The iterator variables in the for loops don't match the indexing variables...
The code for the 4 nested loops is below:
m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(5,length(m)*length(m)*length(m));
for A1 = m
for A2 = m
for A3 = m
for A4 = n
tic
SmallMAX(A1,A2,A3,A4)
toc;
for i=1:numel(eltime)
for j = 1:length(n)
eltime(j,i) = toc;
end
end
end
end
end
end
The code for the main program is excerpted below:
function [k,test] = SmallMAX(A1,A2,A3,A4)
...
end
Thanks for any help.
In your case, the easiest way is to use A1, A2, A3 and A4 as counters instead of the actual values. This way you them to index the entries of eltime. We can then easily calculate the index in the second dimension with sub2ind and use A4 to index the first dimension of eltime. We need to adjust the arguments in SmallMAX as well.
Here is the code of the proposed method:
m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(length(n),length(m)*length(m)*length(m));
res_k = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));
res_test = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));
for A1 = 1:length(m)
for A2 = 1:length(m)
for A3 = 1:length(m)
for A4 = 1:length(n)
ind = sub2ind([length(m),length(m),length(m)],A3,A2,A1);
tic
[k,test] = SmallMAX(m(A1),m(A2),m(A3),n(A4));
eltime(A4,ind) = toc;
res_k(A4,ind) = k;
res_test(A4,ind) = test;
end
end
end
end
This is the order of the addressed entries of eltime:
eltime_order =
Columns 1 through 18
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86
2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 82 87
3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88
4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90
Columns 19 through 36
91 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176
92 97 102 107 112 117 122 127 132 137 142 147 152 157 162 167 172 177
93 98 103 108 113 118 123 128 133 138 143 148 153 158 163 168 173 178
94 99 104 109 114 119 124 129 134 139 144 149 154 159 164 169 174 179
95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180
Columns 37 through 54
181 186 191 196 201 206 211 216 221 226 231 236 241 246 251 256 261 266
182 187 192 197 202 207 212 217 222 227 232 237 242 247 252 257 262 267
183 188 193 198 203 208 213 218 223 228 233 238 243 248 253 258 263 268
184 189 194 199 204 209 214 219 224 229 234 239 244 249 254 259 264 269
185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270
Columns 55 through 72
271 276 281 286 291 296 301 306 311 316 321 326 331 336 341 346 351 356
272 277 282 287 292 297 302 307 312 317 322 327 332 337 342 347 352 357
273 278 283 288 293 298 303 308 313 318 323 328 333 338 343 348 353 358
274 279 284 289 294 299 304 309 314 319 324 329 334 339 344 349 354 359
275 280 285 290 295 300 305 310 315 320 325 330 335 340 345 350 355 360
Columns 73 through 90
361 366 371 376 381 386 391 396 401 406 411 416 421 426 431 436 441 446
362 367 372 377 382 387 392 397 402 407 412 417 422 427 432 437 442 447
363 368 373 378 383 388 393 398 403 408 413 418 423 428 433 438 443 448
364 369 374 379 384 389 394 399 404 409 414 419 424 429 434 439 444 449
365 370 375 380 385 390 395 400 405 410 415 420 425 430 435 440 445 450
Columns 91 through 108
451 456 461 466 471 476 481 486 491 496 501 506 511 516 521 526 531 536
452 457 462 467 472 477 482 487 492 497 502 507 512 517 522 527 532 537
453 458 463 468 473 478 483 488 493 498 503 508 513 518 523 528 533 538
454 459 464 469 474 479 484 489 494 499 504 509 514 519 524 529 534 539
455 460 465 470 475 480 485 490 495 500 505 510 515 520 525 530 535 540
Columns 109 through 125
541 546 551 556 561 566 571 576 581 586 591 596 601 606 611 616 621
542 547 552 557 562 567 572 577 582 587 592 597 602 607 612 617 622
543 548 553 558 563 568 573 578 583 588 593 598 603 608 613 618 623
544 549 554 559 564 569 574 579 584 589 594 599 604 609 614 619 624
545 550 555 560 565 570 575 580 585 590 595 600 605 610 615 620 625

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