creating cell array using find command
조회 수: 1 (최근 30일)
이전 댓글 표시
לק"י
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
답변 (2개)
Star Strider
2023년 7월 23일
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))
ColIdx = cellfun(@(x)x>=2000, analysisdatacd3(6,:)) % Columns With Values >= In Row 6 (Logical VEctor)
analysisdatacd3_new = analysisdatacd3(:,~ColIdx) % Remove Columns With Values >= 2000 In Row 6
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'}
ColIdx = cellfun(@(x)x>=2000, analysisdatacd3(6,:)) % Columns With Values >= In Row 6 (Logical VEctor)
analysisdatacd3_new = analysisdatacd3(:,~ColIdx) % Remove Columns With Values >= 2000 In Row 6
It would help to have your cell arrays, however this should work with them.
.
댓글 수: 0
Paul
2023년 7월 23일
편집: Paul
2023년 7월 23일
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).')
% based on logical indexing
acd3cd8nolykeep = [analysisdataacd3{6,2:end}] < 2000;
acd3cd8noly = analysisdataacd3(1:end,[true acd3cd8nolykeep]) % make sure to keep the first column
% or based on find
acd3cd8nolyfind = find([analysisdataacd3{6,2:end}] < 2000);
acd3cd8noly = analysisdataacd3(1:end,[1, acd3cd8nolyfind+1])
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!