How do I loop over all columns in a cell array?

The following code caluclates the outliers above 3 standard deviations for a single column in balls_data{2,1}(:,1).
data = balls_data{2,1};
mean_val = mean(cat(1, data{:})); % calculate the mean
std_val = std(cat(1, data{:})); % calculate the standard deviation
threshold = 2*std_val; % set the threshold for outlier detection
outliers = cellfun(@(x) x(x > mean_val + threshold | x < mean_val - threshold), data, 'UniformOutput', false); % find the outliers
I now need to run the same code over all columns in balls_data{2,1}.
Could someone help me make this into a loop?
Thanks!

4 comentarios

Voss
Voss el 17 de Mzo. de 2023
Editada: Voss el 17 de Mzo. de 2023
"The following code caluclates the outliers above 3 standard deviations for a single column in balls_data{2,1}(:,1)."
No. The code calculates outliers among all elements of balls_data{2,1}, not just those in the first column.
Anyway, it's easier to work with a numeric array than a cell array containing all scalar numerics.
load('balls_data.mat')
all(cellfun(@isscalar,balls_data{2,1}),'all') % all cells contain a scalar
ans = logical
1
Original cell array approach, with additional code to get the actual outliers from the outliers cell array:
data = balls_data{2,1};
mean_val = mean(cat(1, data{:})); % calculate the mean
std_val = std(cat(1, data{:})); % calculate the standard deviation
threshold = 2*std_val; % set the threshold for outlier detection
outliers = cellfun(@(x) x(x > mean_val + threshold | x < mean_val - threshold), data, 'UniformOutput', false); % find the outliers
outliers = cat(1,outliers{:});
disp(outliers);
1.7301 1.7301 1.7324 1.7334 1.7338 1.7349 1.7349 1.7361 1.7370 1.7378 1.7385 1.7385 1.7392 1.7396 1.7399 1.7400 1.7400 1.7401 1.7400 1.7395 1.7395 1.7404 1.7398 1.7375 1.7373 1.7366 1.7366 1.7357 1.7348 1.7341 1.7341 1.7336 1.7336 1.7343 1.7361 1.7361 1.7361 1.7344 1.7349 1.7363 1.7363 1.7369 1.7359 1.7360 1.7371 1.7371 1.7377 1.7373 1.7366 1.7368 1.7368 1.7374 1.7383 1.7385 1.7387 1.7387 1.7392 1.7394 1.7397 1.7398 1.7398 1.7397 1.7401 1.7421 1.7440 1.7440 1.7433 1.7437 1.7455 1.7458 1.7458 1.7454 1.7458 1.7462 1.7463 1.7463 1.7458 1.7453 1.7447 1.7443 1.7443 1.7439 1.7437 1.7432 1.7430 1.7430 1.7438 1.7445 1.7441 1.7441 1.7441 1.7441 1.7440 1.7443 1.7444 1.7444 1.7442 1.7440 1.7439 1.7444 1.7444 1.7446 1.7443 1.7443 1.7434 1.7434 1.7429 1.7425 1.7422 1.7424 1.7424 1.7429 1.7429 1.7419 1.7402 1.7402 1.7372 1.7330 1.7279 1.7224 1.7224 1.7172 1.7104 1.7043 1.6967 1.6967 1.6906 1.6833 1.6785 1.6760 1.6760 1.6723 1.6690 1.6642 1.6642 1.6588 1.6551 1.6528 1.6515 1.6515 1.6496 1.6474 1.6457 1.6442 1.6442 1.6432 1.6420 1.6411 1.6405 1.6405 1.6406 1.6410 1.6410 1.6409 1.6409 1.6411 1.6423 1.6435 1.6431 1.6431 1.6430 1.6431 1.6427 1.6429 1.6429 1.6435 1.6438 1.6437 1.6437 1.6437 1.6438 1.6435 1.6433 1.6433 1.6433 1.6433 1.6426 1.6416 1.6406 1.6406 1.6402 1.6398 1.6391 1.6381 1.6381 1.6371 1.6359 1.6347 1.6336 1.6336 1.6324 1.6310 1.6292 1.6273 1.6273 1.6252 1.6230 1.6206 1.6180 1.6180 1.6149 1.6115 1.6079 1.6041 1.6041 1.5996 1.5948 1.5900 1.5837 1.5837 1.5773 1.5695 1.5627 1.5544 1.5544 1.5450 1.5357 1.5374 1.5535 1.5535 1.5693 1.5838 1.5972 1.6109 1.6109 1.6233 1.6341 1.6430 1.6514 1.6514 1.6605 1.6695 1.6777 1.6843 1.6843 1.6895 1.6940 1.6985 1.7025 1.7025 1.7063 1.7101 1.7133 1.7161 1.7161 1.7184 1.7200 1.7216 1.7216 1.7236 1.7245 1.7257 1.7270 1.7270 1.7281 1.7290 1.7300 1.7315 1.7315 1.7324 1.7332 1.7345 1.7353 1.7353 1.7358 1.7364 1.7368 1.7369 1.7369 1.7370 1.7377 1.7379 1.7376 1.7376 1.7375 1.7374 1.7369 1.7363 1.7363 1.7359 1.7362 1.7364 1.7366 1.7366 1.7366 1.7367 1.7372 1.7375 1.7375 1.7377 1.7379 1.7383 1.7387 1.7387 1.7387 1.7392 1.7401 1.7402 1.7402 1.7404 1.7412 1.7410 1.7406 1.7406 1.7407 1.7412 1.7416 1.7416 1.7416 1.7425 1.7434 1.7436 1.7440 1.7440 1.7443 1.7447 1.7458 1.7464 1.7464 1.7463 1.7462 1.7460 1.7455 1.7455 1.7451 1.7449 1.7449 1.7450 1.7450 1.7450 1.7451 1.7452 1.7455 1.7455 1.7462 1.7464 1.7465 1.7468 1.7468 1.7470 1.7470 1.7473 1.7476 1.7476 1.7474 1.7475 1.7477 1.7478 1.7478 1.7481 1.7481 1.7481 1.7481 1.7483 1.7485 1.7484 1.7483 1.7483 1.7483 1.7485 1.7489 1.7490 1.7490 1.7489 1.7488 1.7486 1.7484 1.7484 1.7486 1.7484 1.7483 1.7484 1.7484 1.7484 1.7484 1.7482 1.7481 1.7481 1.7479 1.7480 1.7482 1.7482 1.7482 1.7483 1.7483 1.7483 1.7485 1.7485 1.7487 1.7488 1.7490 1.7492 1.7492 1.7494 1.7497 1.7506 1.7511 1.7511 1.7515 1.7524 1.7533 1.7544 1.7544 1.7551 1.7551 1.7558 1.7572 1.7572 1.7584 1.7596 1.7610 1.7618 1.7618 1.7613 1.7613 1.7621 1.7626 1.7626 1.7624 1.7617 1.7612 1.7605 1.7605 1.7600 1.7595 1.7595 1.7598 1.7598 1.7602 1.7599 1.7598 1.7601 1.7601 1.7601 1.7600 1.7598 1.7597 1.7597 1.7595 1.7590 1.7581 1.7573 1.7573 1.7563 1.7556 1.7553 1.7550 1.7550 1.7545 1.7539 1.7532 1.7523 1.7523 1.7512 1.7500 1.7493 1.7493 1.7494 1.7496 1.7492 1.7481 1.7481 1.7473 1.7467 1.7463 1.7463 1.7463 1.7459 1.7455 1.7452 1.7451 1.7451 1.7448 1.7444 1.7439 1.7434 1.7434 1.7432 1.7429 1.7424 1.7420 1.7420 1.7420 1.7421 1.7420 1.7416 1.7416 1.7415 1.7412 1.7410 1.7408 1.7408 1.7404 1.7402 1.7403 1.7403 1.7403 1.7401 1.7400 1.7399 1.7397 1.7397 1.7392 1.7385 1.7375 1.7365 1.7365 1.7356 1.7346 1.7334 1.7324 1.7324 1.7312 1.7296 1.7287 1.7290 1.7290 1.7294 1.7286 1.7272 1.7259 1.7259 1.7246 1.7232 1.7214 1.7194 1.7194 1.7175 1.7162 1.7148 1.7130 1.7130 1.7108 1.7088 1.7064 1.7038 1.7038 1.7008 1.6975 1.6945 1.6917 1.6917 1.6891 1.6862 1.6833 1.6809 1.6809 1.6788 1.6769 1.6752 1.6734 1.6734 1.6714 1.6699 1.6689 1.6677 1.6677 1.6663 1.6649 1.6634 1.6625 1.6625 1.6615 1.6601 1.6586 1.6586 1.6571 1.6556 1.6541 1.6527 1.6509 1.6509 1.6491 1.6474 1.6456 1.6456 1.6431 1.6403 1.6376 1.6350 1.6350 1.6322 1.6292 1.6261 1.6226 1.6226 1.6185 1.6140 1.6096 1.6052 1.6052 1.6000 1.5937 1.5881 1.5815 1.5815 1.5757 1.5678 1.5609 1.5521 1.5521 1.5438 1.5363 1.5354 1.5522 1.5687 1.5687 1.5829 1.5957 1.6088 1.6215 1.6215 1.6330 1.6433 1.6523 1.6594 1.6594 1.6670 1.6742 1.6801 1.6871 1.6871 1.6931 1.6976 1.7021 1.7057 1.7057 1.7088 1.7117 1.7148 1.7168 1.7168 1.7191 1.7213 1.7225 1.7247 1.7247 1.7264 1.7272 1.7282 1.7299 1.7299 1.7311 1.7312 1.7319 1.7329 1.7329 1.7332 1.7338 1.7346 1.7346 1.7340 1.7337 1.7348 1.7352 1.7352 1.7351 1.7354 1.7353 1.7348 1.7348 1.7345 1.7349 1.7354 1.7349 1.7349 1.7350 1.7352 1.7349 1.7352 1.7352 1.7355 1.7354 1.7351 1.7347 1.7347 1.7346 1.7349 1.7352 1.7350 1.7350 1.7349 1.7350 1.7355 1.7360 1.7360 1.7360 1.7357 1.7351 1.7354 1.7354 1.7359 1.7364 1.7366 1.7367 1.7367 1.7368 1.7370 1.7370 1.7366 1.7366 1.7363 1.7361 1.7358 1.7357 1.7357 1.7354 1.7352 1.7351 1.7352 1.7352 1.7353 1.7354 1.7354 1.7354 1.7354 1.7355 1.7358 1.7359 1.7357 1.7357 1.7358 1.7359 1.7359 1.7361 1.7361 1.7361 1.7365 1.7367 1.7365 1.7365 1.7371 1.7380 1.7381 1.7378 1.7378 1.7380 1.7386 1.7390 1.7391 1.7391 1.7391 1.7389 1.7390 1.7393 1.7393 1.7394 1.7395 1.7395 1.7395 1.7390 1.7388 1.7389 1.7390 1.7389 1.7389 1.7387 1.7384 1.7379 1.7379 1.7370 1.7358 1.7344 1.7331 1.7331 1.7318 1.7308 1.7305 1.7311 1.7311 1.7322 1.7322 1.7316 1.7317 1.7317 1.7316 1.7317 1.7317 1.7317 1.7317 1.7319 1.7321 1.7320 1.7319 1.7319 1.7319 1.7318 1.7315 1.7315 1.7315 1.7318 1.7321 1.7322 1.7323 1.7323 1.7326 1.7328 1.7329 1.7330 1.7330 1.7332 1.7335 1.7339 1.7340 1.7340 1.7340 1.7343 1.7347 1.7350 1.7350 1.7350 1.7353 1.7356 1.7358 1.7358 1.7361 1.7363 1.7362 1.7363 1.7363 1.7366 1.7369 1.7372 1.7374 1.7374 1.7377 1.7380 1.7383 1.7386 1.7386 1.7392 1.7396 1.7400 1.7405 1.7405 1.7411 1.7414 1.7416 1.7422 1.7422 1.7430 1.7436 1.7438 1.7439 1.7439 1.7441 1.7444 1.7448 1.7452 1.7452 1.7457 1.7458 1.7461 1.7470 1.7470 1.7477 1.7482 1.7491 1.7491 1.7500 1.7507 1.7515 1.7518 1.7518 1.7517 1.7521 1.7534 1.7538 1.7538 1.7526 1.7536 1.7557 1.7561 1.7561 1.7551 1.7553 1.7562 1.7573 1.7573 1.7577 1.7576 1.7572 1.7560 1.7560 1.7547 1.7541 1.7545 1.7550 1.7550 1.7550 1.7546 1.7543 1.7545 1.7545 1.7553 1.7558 1.7557 1.7555 1.7555 1.7554 1.7552 1.7546 1.7543 1.7543 1.7544 1.7543 1.7536 1.7527 1.7527 1.7524 1.7524 1.7524 1.7521 1.7521 1.7511 1.7502 1.7494 1.7490 1.7490 1.7486 1.7479 1.7471 1.7470 1.7470 1.7471 1.7468 1.7460 1.7451 1.7451 1.7444 1.7441 1.7441 1.7439 1.7439 1.7433 1.7429 1.7420 1.7408 1.7408 1.7397 1.7393 1.7395 1.7398 1.7398 1.7391 1.7383 1.7377 1.7374 1.7374 1.7375 1.7375 1.7370 1.7365 1.7365 1.7362 1.7361 1.7359 1.7357 1.7357 1.7353 1.7350 1.7348 1.7348 1.7345 1.7341 1.7336 1.7330 1.7330 1.5385 1.5514 1.5619 1.5720 1.5714 1.5773 1.5821 1.5884 1.5876 1.5919 1.5937 1.5949 1.5977 1.5944 1.5938 1.5916 1.5889 1.5881 1.5857 1.5839 1.5803 1.5757 1.5732 1.5873 1.5848 1.5815 1.5788 1.5778 1.5703 1.5536 1.5373 1.5459 1.5692 1.5864 1.5915 1.6077 1.6184 1.6257 1.6353 1.6288 1.6289 1.6282 1.6301 1.6323 1.6277 1.6209 1.6147 1.6082 1.6040 1.6008 1.5945 1.5861 1.5791 1.5735 1.5718 1.5653 1.5593 1.5534 1.5483 1.5486 1.5436 1.5389 1.5384 1.5603 1.5833 1.6069 1.6234 1.6291 1.6474 1.6622 1.6729 1.6843 1.6770 1.6829 1.6783 1.6760 1.6773 1.6729 1.6688 1.6649 1.6598 1.6570 1.6516 1.6410 1.6315 1.6218 1.6145 1.6121 1.6040 1.5980 1.5930 1.5877 1.5888 1.5846 1.5807 1.5770 1.5733 1.5733 1.5701 1.5670 1.5645 1.5620 1.5626 1.5602 1.5587 1.5571 1.5553 1.5553 1.5536 1.5508 1.5479 1.5456 1.5440 1.5401 1.5360
Numeric array (in this case a simple vector) approach:
data = cat(1, balls_data{2,1}{:}); % make a column vector, then everything else is easier
mean_val = mean(data); % calculate the mean
std_val = std(data); % calculate the standard deviation
threshold = 2*std_val; % set the threshold for outlier detection
outliers2 = data(data > mean_val + threshold | data < mean_val - threshold);
disp(outliers2);
1.7301 1.7301 1.7324 1.7334 1.7338 1.7349 1.7349 1.7361 1.7370 1.7378 1.7385 1.7385 1.7392 1.7396 1.7399 1.7400 1.7400 1.7401 1.7400 1.7395 1.7395 1.7404 1.7398 1.7375 1.7373 1.7366 1.7366 1.7357 1.7348 1.7341 1.7341 1.7336 1.7336 1.7343 1.7361 1.7361 1.7361 1.7344 1.7349 1.7363 1.7363 1.7369 1.7359 1.7360 1.7371 1.7371 1.7377 1.7373 1.7366 1.7368 1.7368 1.7374 1.7383 1.7385 1.7387 1.7387 1.7392 1.7394 1.7397 1.7398 1.7398 1.7397 1.7401 1.7421 1.7440 1.7440 1.7433 1.7437 1.7455 1.7458 1.7458 1.7454 1.7458 1.7462 1.7463 1.7463 1.7458 1.7453 1.7447 1.7443 1.7443 1.7439 1.7437 1.7432 1.7430 1.7430 1.7438 1.7445 1.7441 1.7441 1.7441 1.7441 1.7440 1.7443 1.7444 1.7444 1.7442 1.7440 1.7439 1.7444 1.7444 1.7446 1.7443 1.7443 1.7434 1.7434 1.7429 1.7425 1.7422 1.7424 1.7424 1.7429 1.7429 1.7419 1.7402 1.7402 1.7372 1.7330 1.7279 1.7224 1.7224 1.7172 1.7104 1.7043 1.6967 1.6967 1.6906 1.6833 1.6785 1.6760 1.6760 1.6723 1.6690 1.6642 1.6642 1.6588 1.6551 1.6528 1.6515 1.6515 1.6496 1.6474 1.6457 1.6442 1.6442 1.6432 1.6420 1.6411 1.6405 1.6405 1.6406 1.6410 1.6410 1.6409 1.6409 1.6411 1.6423 1.6435 1.6431 1.6431 1.6430 1.6431 1.6427 1.6429 1.6429 1.6435 1.6438 1.6437 1.6437 1.6437 1.6438 1.6435 1.6433 1.6433 1.6433 1.6433 1.6426 1.6416 1.6406 1.6406 1.6402 1.6398 1.6391 1.6381 1.6381 1.6371 1.6359 1.6347 1.6336 1.6336 1.6324 1.6310 1.6292 1.6273 1.6273 1.6252 1.6230 1.6206 1.6180 1.6180 1.6149 1.6115 1.6079 1.6041 1.6041 1.5996 1.5948 1.5900 1.5837 1.5837 1.5773 1.5695 1.5627 1.5544 1.5544 1.5450 1.5357 1.5374 1.5535 1.5535 1.5693 1.5838 1.5972 1.6109 1.6109 1.6233 1.6341 1.6430 1.6514 1.6514 1.6605 1.6695 1.6777 1.6843 1.6843 1.6895 1.6940 1.6985 1.7025 1.7025 1.7063 1.7101 1.7133 1.7161 1.7161 1.7184 1.7200 1.7216 1.7216 1.7236 1.7245 1.7257 1.7270 1.7270 1.7281 1.7290 1.7300 1.7315 1.7315 1.7324 1.7332 1.7345 1.7353 1.7353 1.7358 1.7364 1.7368 1.7369 1.7369 1.7370 1.7377 1.7379 1.7376 1.7376 1.7375 1.7374 1.7369 1.7363 1.7363 1.7359 1.7362 1.7364 1.7366 1.7366 1.7366 1.7367 1.7372 1.7375 1.7375 1.7377 1.7379 1.7383 1.7387 1.7387 1.7387 1.7392 1.7401 1.7402 1.7402 1.7404 1.7412 1.7410 1.7406 1.7406 1.7407 1.7412 1.7416 1.7416 1.7416 1.7425 1.7434 1.7436 1.7440 1.7440 1.7443 1.7447 1.7458 1.7464 1.7464 1.7463 1.7462 1.7460 1.7455 1.7455 1.7451 1.7449 1.7449 1.7450 1.7450 1.7450 1.7451 1.7452 1.7455 1.7455 1.7462 1.7464 1.7465 1.7468 1.7468 1.7470 1.7470 1.7473 1.7476 1.7476 1.7474 1.7475 1.7477 1.7478 1.7478 1.7481 1.7481 1.7481 1.7481 1.7483 1.7485 1.7484 1.7483 1.7483 1.7483 1.7485 1.7489 1.7490 1.7490 1.7489 1.7488 1.7486 1.7484 1.7484 1.7486 1.7484 1.7483 1.7484 1.7484 1.7484 1.7484 1.7482 1.7481 1.7481 1.7479 1.7480 1.7482 1.7482 1.7482 1.7483 1.7483 1.7483 1.7485 1.7485 1.7487 1.7488 1.7490 1.7492 1.7492 1.7494 1.7497 1.7506 1.7511 1.7511 1.7515 1.7524 1.7533 1.7544 1.7544 1.7551 1.7551 1.7558 1.7572 1.7572 1.7584 1.7596 1.7610 1.7618 1.7618 1.7613 1.7613 1.7621 1.7626 1.7626 1.7624 1.7617 1.7612 1.7605 1.7605 1.7600 1.7595 1.7595 1.7598 1.7598 1.7602 1.7599 1.7598 1.7601 1.7601 1.7601 1.7600 1.7598 1.7597 1.7597 1.7595 1.7590 1.7581 1.7573 1.7573 1.7563 1.7556 1.7553 1.7550 1.7550 1.7545 1.7539 1.7532 1.7523 1.7523 1.7512 1.7500 1.7493 1.7493 1.7494 1.7496 1.7492 1.7481 1.7481 1.7473 1.7467 1.7463 1.7463 1.7463 1.7459 1.7455 1.7452 1.7451 1.7451 1.7448 1.7444 1.7439 1.7434 1.7434 1.7432 1.7429 1.7424 1.7420 1.7420 1.7420 1.7421 1.7420 1.7416 1.7416 1.7415 1.7412 1.7410 1.7408 1.7408 1.7404 1.7402 1.7403 1.7403 1.7403 1.7401 1.7400 1.7399 1.7397 1.7397 1.7392 1.7385 1.7375 1.7365 1.7365 1.7356 1.7346 1.7334 1.7324 1.7324 1.7312 1.7296 1.7287 1.7290 1.7290 1.7294 1.7286 1.7272 1.7259 1.7259 1.7246 1.7232 1.7214 1.7194 1.7194 1.7175 1.7162 1.7148 1.7130 1.7130 1.7108 1.7088 1.7064 1.7038 1.7038 1.7008 1.6975 1.6945 1.6917 1.6917 1.6891 1.6862 1.6833 1.6809 1.6809 1.6788 1.6769 1.6752 1.6734 1.6734 1.6714 1.6699 1.6689 1.6677 1.6677 1.6663 1.6649 1.6634 1.6625 1.6625 1.6615 1.6601 1.6586 1.6586 1.6571 1.6556 1.6541 1.6527 1.6509 1.6509 1.6491 1.6474 1.6456 1.6456 1.6431 1.6403 1.6376 1.6350 1.6350 1.6322 1.6292 1.6261 1.6226 1.6226 1.6185 1.6140 1.6096 1.6052 1.6052 1.6000 1.5937 1.5881 1.5815 1.5815 1.5757 1.5678 1.5609 1.5521 1.5521 1.5438 1.5363 1.5354 1.5522 1.5687 1.5687 1.5829 1.5957 1.6088 1.6215 1.6215 1.6330 1.6433 1.6523 1.6594 1.6594 1.6670 1.6742 1.6801 1.6871 1.6871 1.6931 1.6976 1.7021 1.7057 1.7057 1.7088 1.7117 1.7148 1.7168 1.7168 1.7191 1.7213 1.7225 1.7247 1.7247 1.7264 1.7272 1.7282 1.7299 1.7299 1.7311 1.7312 1.7319 1.7329 1.7329 1.7332 1.7338 1.7346 1.7346 1.7340 1.7337 1.7348 1.7352 1.7352 1.7351 1.7354 1.7353 1.7348 1.7348 1.7345 1.7349 1.7354 1.7349 1.7349 1.7350 1.7352 1.7349 1.7352 1.7352 1.7355 1.7354 1.7351 1.7347 1.7347 1.7346 1.7349 1.7352 1.7350 1.7350 1.7349 1.7350 1.7355 1.7360 1.7360 1.7360 1.7357 1.7351 1.7354 1.7354 1.7359 1.7364 1.7366 1.7367 1.7367 1.7368 1.7370 1.7370 1.7366 1.7366 1.7363 1.7361 1.7358 1.7357 1.7357 1.7354 1.7352 1.7351 1.7352 1.7352 1.7353 1.7354 1.7354 1.7354 1.7354 1.7355 1.7358 1.7359 1.7357 1.7357 1.7358 1.7359 1.7359 1.7361 1.7361 1.7361 1.7365 1.7367 1.7365 1.7365 1.7371 1.7380 1.7381 1.7378 1.7378 1.7380 1.7386 1.7390 1.7391 1.7391 1.7391 1.7389 1.7390 1.7393 1.7393 1.7394 1.7395 1.7395 1.7395 1.7390 1.7388 1.7389 1.7390 1.7389 1.7389 1.7387 1.7384 1.7379 1.7379 1.7370 1.7358 1.7344 1.7331 1.7331 1.7318 1.7308 1.7305 1.7311 1.7311 1.7322 1.7322 1.7316 1.7317 1.7317 1.7316 1.7317 1.7317 1.7317 1.7317 1.7319 1.7321 1.7320 1.7319 1.7319 1.7319 1.7318 1.7315 1.7315 1.7315 1.7318 1.7321 1.7322 1.7323 1.7323 1.7326 1.7328 1.7329 1.7330 1.7330 1.7332 1.7335 1.7339 1.7340 1.7340 1.7340 1.7343 1.7347 1.7350 1.7350 1.7350 1.7353 1.7356 1.7358 1.7358 1.7361 1.7363 1.7362 1.7363 1.7363 1.7366 1.7369 1.7372 1.7374 1.7374 1.7377 1.7380 1.7383 1.7386 1.7386 1.7392 1.7396 1.7400 1.7405 1.7405 1.7411 1.7414 1.7416 1.7422 1.7422 1.7430 1.7436 1.7438 1.7439 1.7439 1.7441 1.7444 1.7448 1.7452 1.7452 1.7457 1.7458 1.7461 1.7470 1.7470 1.7477 1.7482 1.7491 1.7491 1.7500 1.7507 1.7515 1.7518 1.7518 1.7517 1.7521 1.7534 1.7538 1.7538 1.7526 1.7536 1.7557 1.7561 1.7561 1.7551 1.7553 1.7562 1.7573 1.7573 1.7577 1.7576 1.7572 1.7560 1.7560 1.7547 1.7541 1.7545 1.7550 1.7550 1.7550 1.7546 1.7543 1.7545 1.7545 1.7553 1.7558 1.7557 1.7555 1.7555 1.7554 1.7552 1.7546 1.7543 1.7543 1.7544 1.7543 1.7536 1.7527 1.7527 1.7524 1.7524 1.7524 1.7521 1.7521 1.7511 1.7502 1.7494 1.7490 1.7490 1.7486 1.7479 1.7471 1.7470 1.7470 1.7471 1.7468 1.7460 1.7451 1.7451 1.7444 1.7441 1.7441 1.7439 1.7439 1.7433 1.7429 1.7420 1.7408 1.7408 1.7397 1.7393 1.7395 1.7398 1.7398 1.7391 1.7383 1.7377 1.7374 1.7374 1.7375 1.7375 1.7370 1.7365 1.7365 1.7362 1.7361 1.7359 1.7357 1.7357 1.7353 1.7350 1.7348 1.7348 1.7345 1.7341 1.7336 1.7330 1.7330 1.5385 1.5514 1.5619 1.5720 1.5714 1.5773 1.5821 1.5884 1.5876 1.5919 1.5937 1.5949 1.5977 1.5944 1.5938 1.5916 1.5889 1.5881 1.5857 1.5839 1.5803 1.5757 1.5732 1.5873 1.5848 1.5815 1.5788 1.5778 1.5703 1.5536 1.5373 1.5459 1.5692 1.5864 1.5915 1.6077 1.6184 1.6257 1.6353 1.6288 1.6289 1.6282 1.6301 1.6323 1.6277 1.6209 1.6147 1.6082 1.6040 1.6008 1.5945 1.5861 1.5791 1.5735 1.5718 1.5653 1.5593 1.5534 1.5483 1.5486 1.5436 1.5389 1.5384 1.5603 1.5833 1.6069 1.6234 1.6291 1.6474 1.6622 1.6729 1.6843 1.6770 1.6829 1.6783 1.6760 1.6773 1.6729 1.6688 1.6649 1.6598 1.6570 1.6516 1.6410 1.6315 1.6218 1.6145 1.6121 1.6040 1.5980 1.5930 1.5877 1.5888 1.5846 1.5807 1.5770 1.5733 1.5733 1.5701 1.5670 1.5645 1.5620 1.5626 1.5602 1.5587 1.5571 1.5553 1.5553 1.5536 1.5508 1.5479 1.5456 1.5440 1.5401 1.5360
isequal(outliers,outliers2)
ans = logical
1
Now, what exactly are you trying to do? The question specifies balls_data{2,1} but the answers given so far iterate over all elements of balls_data, i.e., balls_data{1,1}, balls_data{2,1}, balls_data{3,1}, ...
You want the outliers for each element of balls_data or what?
lil brain
lil brain el 17 de Mzo. de 2023
Editada: lil brain el 17 de Mzo. de 2023
Thanks for your input!
Yes. But next I want to create a new cell array called balls_data_smoothed which is the basically a modified version of balls_data but without the outliers.
So for example, if there is a outlier value in one of the columns in balls_data{2,1} then I want the entire row in that cell containing that value to be deleted.
Sounds like rmoutliers would work for that.
"B = rmoutliers(A) detects and removes outliers from the data in A.
  • If A is a matrix, then rmoutliers detects outliers in each column of A separately and removes the entire row."
data = cell2mat(balls_data{2,1});
data_no_outliers = rmoutliers(data,'mean');
Note that for rmoutliers(_,'mean'), "Outliers are defined as elements more than three standard deviations from the mean." And your code was using two standard deviations.
lil brain
lil brain el 17 de Mzo. de 2023
Editada: lil brain el 17 de Mzo. de 2023
Interesting. This is much simpler of course! What if I would want to remove the outliers for each column in each of the 27 cells in balls_data while skipping over the empty cells?
I want to avoid using means or standard deviations for that matter whch are calculated from all columns in a cell or all cells in balls_data.
Could you be so kind to show me how that code would look?
Thanks!

Iniciar sesión para comentar.

Respuestas (3)

load('balls_data.mat')
balls_data is a cell array of cell arrays, except for two cells, which contain empty 0-by-0 numeric matrices
disp(balls_data)
{ 668×21 cell } {1195×21 cell } {1210×21 cell } {1911×21 cell } { 757×21 cell } { 520×21 cell } {1406×21 cell } {1090×21 cell } {1012×21 cell } { 572×21 cell } { 640×21 cell } {2655×21 cell } {1983×21 cell } { 513×21 cell } { 609×21 cell } {1654×21 cell } {1012×21 cell } { 564×21 cell } { 0×0 double} {1927×21 cell } { 941×21 cell } { 644×21 cell } { 748×21 cell } { 713×21 cell } { 662×21 cell } {2758×21 cell } { 0×0 double}
First, convert the cell arrays of scalars in balls_data to matrices, using cell2mat:
balls_data_mat = cellfun(@cell2mat,balls_data,'UniformOutput',false);
balls_data_mat is a cell array of matrices. Note that cell2mat([]) returns [], so the cells that contained the empty 0-by-0 matrices still contain them. But the other cells contain matrices now. So all cells contain matrices now.
disp(balls_data_mat)
{ 668×21 double} {1195×21 double} {1210×21 double} {1911×21 double} { 757×21 double} { 520×21 double} {1406×21 double} {1090×21 double} {1012×21 double} { 572×21 double} { 640×21 double} {2655×21 double} {1983×21 double} { 513×21 double} { 609×21 double} {1654×21 double} {1012×21 double} { 564×21 double} { 0×0 double} {1927×21 double} { 941×21 double} { 644×21 double} { 748×21 double} { 713×21 double} { 662×21 double} {2758×21 double} { 0×0 double}
Second, remove the rows containing outliers in each matrix:
balls_data_mat_no_outliers = cellfun(@(x)rmoutliers(x,'mean'),balls_data_mat,'UniformOutput',false);
balls_data_mat_no_outliers is a cell array of matrices. Now the matrices each have fewer rows than they did (except the empty ones, which remain empty), because the rows containing any outlier have been removed.
disp(balls_data_mat_no_outliers)
{ 617×21 double} {1169×21 double} {1210×21 double} {1722×21 double} { 757×21 double} { 469×21 double} {1227×21 double} {1042×21 double} {1004×21 double} { 529×21 double} { 609×21 double} {2370×21 double} {1983×21 double} { 466×21 double} { 585×21 double} {1548×21 double} { 969×21 double} { 555×21 double} { 0×0 double} {1828×21 double} { 905×21 double} { 614×21 double} { 666×21 double} { 706×21 double} { 598×21 double} {2706×21 double} { 0×0 double}
Here is the complete code to calculate all outliers:
load('balls_data.mat')
% Note balls_data is a cell array contains some empty cells as well. Thus,
% we need this step to remove them:
balls_data_0=balls_data(~cellfun('isempty',balls_data)); % Remove empty cells
for ii=1:length(balls_data_0)
data = balls_data_0{ii,1};
mean_val = mean(cat(1, data{:})); % calculate the mean
std_val = std(cat(1, data{:})); % calculate the standard deviation
threshold = 2*std_val; % set the threshold for outlier detection
outliers{ii,:} = cellfun(@(x) x(x > mean_val + threshold | x < mean_val - threshold), data, 'UniformOutput', false); % find the outliers
end

4 comentarios

lil brain
lil brain el 16 de Mzo. de 2023
I am actually interested in keeping the empty cells as they are. In that case, how could I skip over them in the loop?
Thanks!
Here is how to keep them there:
load('balls_data.mat')
IDX =~cellfun('isempty',balls_data);
for ii=1:length(balls_data)
if IDX(ii)~=0
data = balls_data{ii,1};
mean_val = mean(cat(1, data{:})); % calculate the mean
std_val = std(cat(1, data{:})); % calculate the standard deviation
threshold = 2*std_val; % set the threshold for outlier detection
outliers{ii,:} = cellfun(@(x) x(x > mean_val + threshold | x < mean_val - threshold), data, 'UniformOutput', false); % find the outliers
else
outliers{ii,:} = []; % Will contain an empty cell where balls_data is empty
end
end
size(outliers)
ans = 1×2
27 1
Sulaymon Eshkabilov
Sulaymon Eshkabilov el 16 de Mzo. de 2023
Editada: Sulaymon Eshkabilov el 17 de Mzo. de 2023
--
lil brain
lil brain el 17 de Mzo. de 2023
thank you very much. However, when I run this code I get:
Assigning to 21 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment.
Error in untitled5 (line 25)
outliers{ii,:} = cellfun(@(x) x(x > mean_val + threshold | x < mean_val - threshold), data, 'UniformOutput', false); % find the outliers
Why is that?

Iniciar sesión para comentar.

Use this code as given here then you will not get the errors which are shown in your message thread:
load('balls_data.mat')
IDX =~cellfun('isempty',balls_data);
for ii=1:length(balls_data)
if IDX(ii)~=0
data = balls_data{ii,1};
mean_val = mean(cat(1, data{:})); % calculate the mean
std_val = std(cat(1, data{:})); % calculate the standard deviation
threshold = 2*std_val; % set the threshold for outlier detection
outliers{ii,:} = cellfun(@(x) x(x > mean_val + threshold | x < mean_val - threshold), data, 'UniformOutput', false); % find the outliers
else
outliers{ii,:} = []; % Will contain an empty cell where balls_data is empty
end
end
size(outliers)
ans = 1×2
27 1
size(balls_data)
ans = 1×2
27 1

2 comentarios

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 17 de Mzo. de 2023
Editada: Sulaymon Eshkabilov el 18 de Mzo. de 2023
Good luck
lil brain
lil brain el 17 de Mzo. de 2023
Sure! But I still get the same error for some reason.
Assigning to 21 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment.

Iniciar sesión para comentar.

Preguntada:

el 16 de Mzo. de 2023

Editada:

el 18 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by