How can I place a cell array in another cell array using 'find'?

2 visualizaciones (últimos 30 días)
Hi, I have a 50x1 cell array (A = cell(50,1)), each cell has a 4x960 matrix(all elements are 0 or 1) in it, I want to this for each cell: use 'find' to extract the indices of elements equal to 1 in that matrix and then use that indices to place that matrix in a cell array 'B' that has 4 rows, 16 blocks of columns and undefined number of elements in that cell(that show the column number of that element equal to 1), here is an example: This is one of my 4x960 matrices: A{1,1} =
1 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
Columns 14 through 26
0 0 0 1 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 1 0 0 0 0 1
Columns 27 through 39
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
Columns 40 through 52
0 1 0 0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0
0 1 0 0 0 0 1 0 0 0 0 1 0
0 1 0 0 0 0 1 0 0 0 0 1 0
Columns 53 through 65
0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 1 0 0 0 0
Columns 66 through 78
0 0 0 0 1 0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 0
Columns 79 through 91
0 1 0 0 0 0 1 0 0 0 0 1 0
1 0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 1 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1
Columns 92 through 104
0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 1 0
Columns 105 through 117
1 0 0 0 0 1 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0 0 0 1 0 0
Columns 118 through 130
0 0 1 0 0 0 0 1 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0
0 0 0 0 0 0 1 0 0 1 0 0 1
0 0 0 1 0 0 0 1 0 0 0 1 0
Columns 131 through 143
1 0 0 1 0 0 1 0 0 1 0 0 1
0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 1 0 0 1 0 0 1 0 0 1 0
0 0 1 0 0 0 1 0 0 0 1 0 0
Columns 144 through 156
0 0 1 0 0 1 0 0 1 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0 0 0
0 1 0 0 1 0 0 1 0 0 1 0 0
0 1 0 0 0 1 0 0 0 1 0 0 0
Columns 157 through 169
0 1 0 0 1 0 0 1 0 0 1 0 0
1 0 0 0 1 0 0 0 1 0 0 0 1
1 0 0 1 0 0 1 0 0 1 0 0 1
1 0 0 0 1 0 0 0 1 0 0 0 1
Columns 170 through 182
1 0 0 1 0 0 1 0 0 1 0 0 1
0 0 0 1 0 0 0 1 0 0 0 1 0
0 0 1 0 0 1 0 0 1 0 0 1 0
0 0 0 1 0 0 0 1 0 0 0 1 0
Columns 183 through 195
0 0 1 0 0 0 1 0 0 0 1 0 0
0 1 0 0 1 0 0 1 0 0 1 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1
Columns 196 through 208
0 1 0 0 0 1 0 0 0 1 0 0 0
1 0 0 1 0 0 1 0 0 1 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0 1 0
Columns 209 through 221
1 0 0 0 1 0 0 0 1 0 0 0 1
0 0 1 0 0 1 0 0 1 0 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1
Columns 222 through 234
0 0 0 1 0 0 0 1 0 0 0 1 0
0 1 0 0 1 0 0 1 0 0 1 0 0
1 0 1 0 1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0 1 0
Columns 235 through 247
0 0 1 0 0 0 1 0 0 0 1 0 0
1 0 0 1 0 0 1 0 0 0 0 0 1
0 1 0 1 0 1 0 1 0 1 0 0 1
1 0 1 0 1 0 1 0 0 0 0 1 0
Columns 248 through 260
1 0 0 1 0 0 1 0 0 1 0 0 1
0 0 0 0 0 1 0 0 0 0 0 1 0
0 0 1 0 0 1 0 0 1 0 0 1 0
0 0 0 1 0 0 0 0 1 0 0 0 0
Columns 261 through 273
0 0 1 0 0 1 0 0 1 0 0 1 0
0 0 0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 1 0 0 1 0 0 1 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
Columns 274 through 286
0 1 0 0 1 0 0 1 0 0 1 0 0
0 0 0 1 0 0 0 0 0 1 0 0 0
1 0 0 1 0 0 1 0 0 1 0 0 1
0 0 1 0 0 0 0 1 0 0 0 0 1
Columns 287 through 299
1 0 0 1 0 0 1 0 0 1 0 0 1
0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 1 0 0 1 0 0 1 0
0 0 0 0 1 0 0 0 0 1 0 0 0
Columns 300 through 312
0 0 1 0 0 1 0 0 0 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0 0 0
0 1 0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 1 0 0 0 1 0 0 0
Columns 313 through 325
0 0 0 0 1 0 0 0 0 0 1 0 0
1 0 0 0 1 0 0 0 1 0 0 0 1
0 0 0 1 0 0 0 0 0 1 0 0 0
1 0 0 0 1 0 0 0 1 0 0 0 1
Columns 326 through 338
0 0 0 1 0 0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0
Columns 339 through 351
0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 1 0 0
Columns 352 through 364
0 1 0 0 0 0 0 1 0 0 0 0 0
0 1 0 0 0 1 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1
0 1 0 0 0 1 0 0 0 1 0 0 0
Columns 365 through 377
1 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0
Columns 378 through 390
0 0 0 1 0 0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 1 0 0 0 0 1 0
0 0 0 1 0 0 0 0 1 0 0 0 0
Columns 391 through 403
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0 1 0 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
Columns 404 through 416
0 1 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0 0 0 1
Columns 417 through 429
0 0 0 0 1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 1 0 0
Columns 430 through 442
0 0 0 0 0 1 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 1 0 0 0 0 0 1 0 0 0
Columns 443 through 455
0 0 0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0 0 0 0
Columns 456 through 468
0 0 0 1 0 0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0 0 0 0 0
Columns 469 through 481
0 0 1 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1
Columns 482 through 494
0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
Columns 495 through 507
1 0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 1 0 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0 0 0 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0
Columns 508 through 520
0 0 0 0 0 1 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 1 0 0 0 0 1 0 0 0 0
Columns 521 through 533
0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0 0 0 0 1 0
1 0 0 0 0 1 0 0 0 0 1 0 0
Columns 534 through 546
0 0 0 1 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0 0 0 0
Columns 547 through 559
0 0 1 0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0 0 0 1
1 0 0 0 0 0 1 0 0 0 0 0 1
Columns 560 through 572
0 0 0 1 0 0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 0 0 1 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0
Columns 573 through 585
0 0 0 0 1 0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 1 0 0
Columns 586 through 598
0 0 0 0 0 1 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0 0 0 0 1 0
0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 1 0 0 0
Columns 599 through 611
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 1 0 0 0 0
1 0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 1 0 0 0 0 1
Columns 612 through 624
1 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
Columns 625 through 637
0 0 0 1 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 0 1 0 0 0 0 0 0 0 1 0
0 1 0 0 0 0 1 0 0 0 0 1 0
Columns 638 through 650
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 1 0 0 0 0
Columns 651 through 663
0 1 0 0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0
0 1 0 0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
Columns 664 through 676
0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 1 0 0 0 1
1 0 0 1 0 0 1 0 0 1 0 0 1
Columns 677 through 689
0 0 0 1 0 0 0 1 0 0 0 1 0
1 0 0 0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0
0 0 1 0 0 1 0 0 1 0 0 1 0
Columns 690 through 702
0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 1 0 0 0 1 0 0 0 1 0 0
0 1 0 0 1 0 0 1 0 0 1 0 0
Columns 703 through 715
0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0 0 1 0 0 0
1 0 0 1 0 0 1 0 0 1 0 0 1
Columns 716 through 728
1 0 0 0 1 0 0 0 1 0 0 0 1
0 1 0 0 0 0 1 0 0 0 0 1 0
1 0 0 0 1 0 0 0 1 0 0 0 1
0 0 1 0 0 1 0 0 0 1 0 0 0
Columns 729 through 741
0 0 0 1 0 0 0 1 0 0 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 1 0 0 1 0 0 1 0 0 1 0
1 0 0 0 1 0 0 0 1 0 0 0 1
Columns 742 through 754
0 0 1 0 0 0 1 0 0 0 1 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 0 1 0 0 1 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0
Columns 755 through 767
0 1 0 0 0 1 0 0 0 1 0 0 0
1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 0 1 0 0 1 0 0 1 0 0 1
0 0 1 0 0 0 1 0 0 0 1 0 0
Columns 768 through 780
1 0 0 0 1 0 0 0 1 0 0 0 1
0 1 0 1 0 1 0 1 0 1 0 1 0
0 0 1 0 0 1 0 0 1 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0 0 0
Columns 781 through 793
0 0 0 1 0 0 0 1 0 0 0 1 0
1 0 1 0 1 0 1 0 0 0 0 1 0
0 1 0 0 1 0 0 1 0 0 0 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1
Columns 794 through 806
0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0
Columns 807 through 819
0 1 0 0 0 1 0 0 0 1 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1
Columns 820 through 832
1 0 0 0 1 0 0 0 1 0 0 0 1
0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 0 1 0 0 0 0 1 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0
Columns 833 through 845
0 0 0 1 0 0 0 1 0 0 0 1 0
0 0 0 0 1 0 0 0 0 1 0 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0
Columns 846 through 858
0 0 1 0 0 0 1 0 0 0 1 0 0
0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 0 1 0 0 0 1 0 0
1 0 0 0 0 1 0 0 0 0 1 0 0
Columns 859 through 871
0 0 0 1 0 0 0 0 0 1 0 0 0
1 0 0 0 1 0 0 0 1 0 0 0 1
0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0 0 0 1
Columns 872 through 884
0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0
1 0 0 0 1 0 0 0 1 0 0 0 1
0 0 0 0 1 0 0 0 0 1 0 0 0
Columns 885 through 897
0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0
0 1 0 0 0 0 1 0 0 0 0 1 0
Columns 898 through 910
1 0 0 0 0 0 1 0 0 0 0 0 1
0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 1 0 0 0
Columns 911 through 923
0 0 0 0 0 1 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 1 0 0 0 0
Columns 924 through 936
0 0 1 0 0 0 0 1 0 0 0 0 1
0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 0
Columns 937 through 949
0 0 0 0 1 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1
Columns 950 through 960
0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
in the first row, in the first 60 columns I want the column number of elements equal to 1(1,9,17,...)(They are not evenly spaced), to place them in the first row and first column of array B = cell(4,16) (16*60=960). Of course I don't know how many 1s are in the first row and first 60 columns so I don't know the length of B(1,1) and then the indices that are in the first row and the next 60 columns (61 to 120) equal to 1 go to B(1,2) and so on. and this happens to other rows of A{1,1} and it also happens to the rest of 49 matrices in A(50,1). Sorry if I couldn't explain well. Would you please help me do this?
  1 comentario
Jan
Jan el 4 de Nov. de 2016
Editada: Jan el 4 de Nov. de 2016
Come on, Sherwin. It is a mess to scroll to this pile of values to find the body of your question. We cannot even copy&paste the values to try, if a suggestion works. Please keep your question as lean as possible to improve the readability. Thanks.
I cannot follow the description of the problem. A small example with a few values would be helpful.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 4 de Nov. de 2016
If I understood correctly:
Ax = randi([0 1], 4, 960); %Demo data. No way I'm reformating your example to make it valid matlab syntax.
[row, col] = find(A);
blockidx = discretize(col, 1:60:size(A, 2));
Bx = accumarray([row, blockidx], col, [], @(c) {sort(c).'})
Note: I'm not working on the whole cell array here, just one of these 4x960 matrix as I've not understood what that had to do with the question. Possibly, you just want to above in a loop (or cellfun) operating over each element of the cell array.
  3 comentarios
Guillaume
Guillaume el 5 de Nov. de 2016
Editada: Guillaume el 7 de Nov. de 2016
The c is just an arbitrary name for the input of the anonymous function. It will receive in turn the elements of col that go in a particular block. The @(c) {sort(c).'} anonymous function is equivalent to:
function out = functionwithnoname(c)
out = {sort(c).'};
end
find finds the non zero elements of a matrix. Since your matrix is made of 0 and 1, find(A) and find(A == 1) are exactly the same. Calculating A == 1 is a waste of time in any case, since the result of that comparison is A.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by