creating cell array using find command

1 view (last 30 days)
Amit Ifrach
Amit Ifrach on 23 Jul 2023
Edited: Paul on 23 Jul 2023
לק"י
Hello!
I got cell array that contain alot of data. I want to create a new cell array, identical to the original one, but only lack the columns that have values greater then 2000 in their 6th row. the array looks like this:
I thought to use the find command to excute it, and it works only for the non-char columns (2:end).
The code I used:
acd3cd8nolyfind=find(([analysisdataacd3{6,2:end}])<2000);
acd3cd8noly={};
acd3cd8noly=analysisdataacd3(1:end,acd3cd8nolyfind+1);
It gave only the numbers (which is usefull but not quite what I want):
I tried to concatenate the analysisdataacd3(1:end,1) array into it, but it just gives cell array that contains 2 cell arrays (C1,C2) and not cell arrat that contains in it all the values of those two cell arrays.
How can I make the cell array I want please?
Thanks,
Amit.
  2 Comments
Dyuman Joshi
Dyuman Joshi on 23 Jul 2023
Do you want to remove columns in all rows or only columns in the 6th row?
Amit Ifrach
Amit Ifrach on 23 Jul 2023
לק"י
Hi Joshi, thanks for the reply.
I don't thin I quite understood you.
I just want to create new cell array where the columns that have in the 6th row value greatr than 2000 are missing. the wholre column with all its inputs.
Amit.

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 23 Jul 2023
The find function is not necessary in this instance. Use ‘logical indexing’ to accomplish the same result without the find call —
analysisdatacd3 = num2cell(randi(3000, 10, 15))
analysisdatacd3 = 10×15 cell array
{[1020]} {[2993]} {[1992]} {[2290]} {[2340]} {[1816]} {[2968]} {[ 36]} {[ 666]} {[ 579]} {[1315]} {[ 987]} {[ 882]} {[1310]} {[1873]} {[ 866]} {[ 665]} {[1767]} {[2598]} {[2973]} {[1971]} {[ 80]} {[ 752]} {[ 13]} {[ 872]} {[2529]} {[2877]} {[2769]} {[1950]} {[2927]} {[ 56]} {[1773]} {[ 556]} {[1952]} {[ 83]} {[2664]} {[1932]} {[1823]} {[ 636]} {[ 485]} {[1018]} {[2142]} {[2168]} {[ 812]} {[ 923]} {[2805]} {[1899]} {[1592]} {[ 24]} {[ 659]} {[1408]} {[ 913]} {[2363]} {[ 770]} {[1375]} {[ 389]} {[2654]} {[1165]} {[ 729]} {[2664]} {[1911]} {[2656]} {[ 308]} {[1918]} {[ 773]} {[2936]} {[ 203]} {[1156]} {[1335]} {[ 514]} {[2112]} {[1662]} {[1772]} {[ 26]} {[2626]} {[1521]} {[ 429]} {[2215]} {[1041]} {[2546]} {[1922]} {[1718]} {[2650]} {[ 677]} {[2214]} {[2514]} {[2930]} {[2527]} {[2669]} {[ 956]} {[ 188]} {[2838]} {[1473]} {[ 775]} {[ 493]} {[1559]} {[2629]} {[2175]} {[2773]} {[1242]} {[ 670]} {[ 110]} {[2769]} {[ 416]} {[2040]} {[ 339]} {[1417]} {[2563]} {[2808]} {[1196]} {[1773]} {[ 505]} {[2737]} {[1293]} {[ 510]} {[2937]} {[1456]} {[1174]} {[2038]} {[2157]} {[ 642]} {[ 974]} {[2489]} {[2545]} {[1974]} {[2123]} {[1222]} {[2346]} {[ 385]} {[ 405]} {[ 846]} {[2021]} {[ 521]} {[ 528]} {[2664]} {[2952]} {[2119]} {[ 483]} {[ 345]} {[2704]} {[1331]} {[2656]} {[2769]} {[1503]} {[ 297]} {[1517]} {[ 616]} {[2169]} {[1989]} {[ 294]}
ColIdx = cellfun(@(x)x>=2000, analysisdatacd3(6,:)) % Columns With Values >= In Row 6 (Logical VEctor)
ColIdx = 1×15 logical array
0 0 1 0 1 0 0 1 0 1 1 1 1 1 0
analysisdatacd3_new = analysisdatacd3(:,~ColIdx) % Remove Columns With Values >= 2000 In Row 6
analysisdatacd3_new = 10×7 cell array
{[1020]} {[2993]} {[2290]} {[1816]} {[2968]} {[ 666]} {[1873]} {[ 866]} {[ 665]} {[2598]} {[1971]} {[ 80]} {[ 13]} {[2927]} {[ 56]} {[1773]} {[1952]} {[2664]} {[1932]} {[ 636]} {[ 923]} {[2805]} {[1899]} {[ 24]} {[1408]} {[ 913]} {[ 770]} {[2664]} {[1911]} {[2656]} {[1918]} {[2936]} {[ 203]} {[1335]} {[2626]} {[1521]} {[ 429]} {[1041]} {[1922]} {[1718]} {[ 677]} {[ 956]} {[ 188]} {[2838]} {[ 775]} {[1559]} {[2629]} {[2773]} {[2040]} {[ 339]} {[1417]} {[2808]} {[1773]} {[ 505]} {[1293]} {[2157]} {[ 642]} {[ 974]} {[2545]} {[2123]} {[1222]} {[ 385]} {[2664]} {[2952]} {[2119]} {[ 345]} {[1331]} {[2656]} {[1503]} {[ 294]}
The logical vector ‘ColIdx’ returns true (1) for the columns that meet the criterion. The cellfun funciton is necessary to do this with cell arrays. The indexing to produce the desired result is then straightforward.
Doing this with the first cell not being numeric is also straightforward —
analysisdatacd3 = num2cell(randi(3000, 10, 15));
analysisdatacd3(:,1) = {'a','b','c','d','e','f','g','h','i','j'}
analysisdatacd3 = 10×15 cell array
{'a'} {[2195]} {[2261]} {[ 243]} {[2187]} {[1485]} {[ 134]} {[1841]} {[1866]} {[1953]} {[2933]} {[ 898]} {[2165]} {[1406]} {[ 899]} {'b'} {[1432]} {[ 990]} {[2655]} {[ 762]} {[1801]} {[1880]} {[1361]} {[1306]} {[1924]} {[1492]} {[2875]} {[1516]} {[2076]} {[ 187]} {'c'} {[2869]} {[2173]} {[ 188]} {[2100]} {[ 954]} {[2371]} {[ 407]} {[ 299]} {[1378]} {[1014]} {[ 803]} {[1667]} {[2167]} {[1358]} {'d'} {[2903]} {[2660]} {[ 337]} {[ 573]} {[1982]} {[ 541]} {[1938]} {[1847]} {[2963]} {[2737]} {[1938]} {[1971]} {[2682]} {[2191]} {'e'} {[ 794]} {[ 482]} {[ 628]} {[ 901]} {[ 502]} {[ 837]} {[ 435]} {[ 553]} {[2306]} {[2336]} {[1719]} {[1621]} {[1820]} {[2854]} {'f'} {[ 964]} {[1933]} {[2742]} {[1541]} {[ 116]} {[2815]} {[ 703]} {[2681]} {[ 551]} {[1104]} {[ 665]} {[2373]} {[1644]} {[ 861]} {'g'} {[2699]} {[ 923]} {[1241]} {[ 890]} {[ 730]} {[ 446]} {[1678]} {[1129]} {[2713]} {[1797]} {[2353]} {[1180]} {[2902]} {[ 423]} {'h'} {[1425]} {[ 125]} {[1367]} {[2389]} {[1101]} {[1056]} {[1858]} {[ 370]} {[1348]} {[ 235]} {[2672]} {[ 993]} {[ 766]} {[1426]} {'i'} {[2369]} {[ 55]} {[2050]} {[1892]} {[2816]} {[2079]} {[ 244]} {[ 504]} {[1460]} {[ 996]} {[ 295]} {[ 400]} {[2775]} {[ 232]} {'j'} {[1581]} {[ 182]} {[1392]} {[2490]} {[2354]} {[1896]} {[2801]} {[1312]} {[2604]} {[1478]} {[ 632]} {[2092]} {[1782]} {[1719]}
ColIdx = cellfun(@(x)x>=2000, analysisdatacd3(6,:)) % Columns With Values >= In Row 6 (Logical VEctor)
ColIdx = 1×15 logical array
0 0 0 1 0 0 1 0 1 0 0 0 1 0 0
analysisdatacd3_new = analysisdatacd3(:,~ColIdx) % Remove Columns With Values >= 2000 In Row 6
analysisdatacd3_new = 10×11 cell array
{'a'} {[2195]} {[2261]} {[2187]} {[1485]} {[1841]} {[1953]} {[2933]} {[ 898]} {[1406]} {[ 899]} {'b'} {[1432]} {[ 990]} {[ 762]} {[1801]} {[1361]} {[1924]} {[1492]} {[2875]} {[2076]} {[ 187]} {'c'} {[2869]} {[2173]} {[2100]} {[ 954]} {[ 407]} {[1378]} {[1014]} {[ 803]} {[2167]} {[1358]} {'d'} {[2903]} {[2660]} {[ 573]} {[1982]} {[1938]} {[2963]} {[2737]} {[1938]} {[2682]} {[2191]} {'e'} {[ 794]} {[ 482]} {[ 901]} {[ 502]} {[ 435]} {[2306]} {[2336]} {[1719]} {[1820]} {[2854]} {'f'} {[ 964]} {[1933]} {[1541]} {[ 116]} {[ 703]} {[ 551]} {[1104]} {[ 665]} {[1644]} {[ 861]} {'g'} {[2699]} {[ 923]} {[ 890]} {[ 730]} {[1678]} {[2713]} {[1797]} {[2353]} {[2902]} {[ 423]} {'h'} {[1425]} {[ 125]} {[2389]} {[1101]} {[1858]} {[1348]} {[ 235]} {[2672]} {[ 766]} {[1426]} {'i'} {[2369]} {[ 55]} {[1892]} {[2816]} {[ 244]} {[1460]} {[ 996]} {[ 295]} {[2775]} {[ 232]} {'j'} {[1581]} {[ 182]} {[2490]} {[2354]} {[2801]} {[2604]} {[1478]} {[ 632]} {[1782]} {[1719]}
It would help to have your cell arrays, however this should work with them.
.

Paul
Paul on 23 Jul 2023
Edited: Paul on 23 Jul 2023
Hi Amit,
Just need to prepend the index vector to keep the first column.
rng(100)
analysisdataacd3 = num2cell(randi(3000, 10, 5));
analysisdataacd3(:,1) = cellstr(char(97:106).')
analysisdataacd3 = 10×5 cell array
{'a'} {[2674]} {[1296]} {[1797]} {[2228]} {'b'} {[ 628]} {[2821]} {[1812]} {[1891]} {'c'} {[ 556]} {[2453]} {[ 316]} {[1746]} {'d'} {[ 326]} {[1009]} {[1146]} {[ 62]} {'e'} {[ 660]} {[ 527]} {[ 110]} {[ 631]} {'f'} {[2936]} {[1119]} {[2672]} {[1635]} {'g'} {[2436]} {[ 18]} {[2943]} {[2308]} {'h'} {[ 516]} {[ 758]} {[ 180]} {[ 753]} {'i'} {[2449]} {[2387]} {[2672]} {[ 858]} {'j'} {[ 823]} {[ 46]} {[1731]} {[2558]}
% based on logical indexing
acd3cd8nolykeep = [analysisdataacd3{6,2:end}] < 2000;
acd3cd8noly = analysisdataacd3(1:end,[true acd3cd8nolykeep]) % make sure to keep the first column
acd3cd8noly = 10×3 cell array
{'a'} {[1296]} {[2228]} {'b'} {[2821]} {[1891]} {'c'} {[2453]} {[1746]} {'d'} {[1009]} {[ 62]} {'e'} {[ 527]} {[ 631]} {'f'} {[1119]} {[1635]} {'g'} {[ 18]} {[2308]} {'h'} {[ 758]} {[ 753]} {'i'} {[2387]} {[ 858]} {'j'} {[ 46]} {[2558]}
% or based on find
acd3cd8nolyfind = find([analysisdataacd3{6,2:end}] < 2000);
acd3cd8noly = analysisdataacd3(1:end,[1, acd3cd8nolyfind+1])
acd3cd8noly = 10×3 cell array
{'a'} {[1296]} {[2228]} {'b'} {[2821]} {[1891]} {'c'} {[2453]} {[1746]} {'d'} {[1009]} {[ 62]} {'e'} {[ 527]} {[ 631]} {'f'} {[1119]} {[1635]} {'g'} {[ 18]} {[2308]} {'h'} {[ 758]} {[ 753]} {'i'} {[2387]} {[ 858]} {'j'} {[ 46]} {[2558]}

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!