필터 지우기
필터 지우기

How do I convert to nan all entries with a particular value in a cell array?

조회 수: 3 (최근 30일)
Garima Sharma
Garima Sharma 2017년 6월 14일
댓글: Star Strider 2017년 6월 15일
Each entry of the cell array is a matrix, and I want to convert to nan all entries, say, of value=1. Something like this: a=cellfun(@(x) x(x==1)=nan, a, 'un',0)
I tried to do this by creating a separate function, but I need to delete different values from the matrix assigned to each cell in the cell array, so I'm not sure how that would work. Can I just define a cell function like this?
function out = replaceval(in, val)
in(in==val) = NaN;
out = in;
end
  댓글 수: 1
Rik
Rik 2017년 6월 14일
This should work as far as I can tell. Keep in mind that you might have to replicate your second input as a cell if it is not the same for the cell you are using as first input.

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2017년 6월 14일
I could not get this to work with only cellfun calls, and needed to use a loop.
This works:
C = {randi(9,5), randi(9,5), randi(9, 5)}; % Create Data
L = cellfun(@(x) x == 1, C, 'Uni',0); % Logical Arrays
C1 = C{1} % Display Initial Values
L1 = L{1} % Display Logical Arrays
for k1 = 1:size(C,2)
C{k1}(L{k1}) = NaN; % Replace With ‘NaN’
end
New_C1 = C{1} % Display Replaced Values
C1 =
2 4 9 3 5
1 8 1 3 7
4 5 2 3 4
5 3 7 6 1
6 5 7 6 8
L1 =
5×5 logical array
0 0 0 0 0
1 0 1 0 0
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
New_C1 =
2 4 9 3 5
NaN 8 NaN 3 7
4 5 2 3 4
5 3 7 6 NaN
6 5 7 6 8
Remove the ‘Display’ lines. They exist only to demonstrate the results.
  댓글 수: 2
Garima Sharma
Garima Sharma 2017년 6월 15일
Actually, this worked for me. I just have to make sure that the 2 cell arrays from which in and val are pulled have the same dimension.
B=cellfun(@replaceval, a, b, 'un',0)
function out = replaceval(in, val)
in(in==val) = NaN;
out = in;
end
Star Strider
Star Strider 2017년 6월 15일
In my code, the logical cell arrays ‘L’ will by definition have the same dimensions as the matrices they are derived from. That is the advantage of calculating them in a separate step.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by