在单元格数组中查找并替换值 [英] find and replace values in cell array

查看:151
本文介绍了在单元格数组中查找并替换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的单元格数组:[...

I have a cell array like this: [...

0
129
8 ... 2 ... 3 ... 4
6 ... 4
0

0
129
8...2...3...4
6...4
0

我只想查找和替换特定的值,但是我不能使用普通函数,因为单元格的长度不同.我需要同时替换许多特定值,并且没有关于如何替换值的常规功能.但是,有时几个输入值应替换为同一输出.

I just want to find and replace specific values, but I can't use the ordinary function because the cells are different lengths. I need to replace many specific values at the same time and there is no general function about how values are replaced. However, sometimes several input values should be replaced by the same output.

所以我想说
对于值1:129
'如果为0,则为9'
'elseif 1 then 50'
'elseif 2或3或4然后61' 等等...最多129

so I want to say
for values 1:129
'if 0, then 9'
'elseif 1 then 50'
'elseif 2 or 3 or 4 then 61' etc...up to 129

这些规则应用于整个数组.

where these rules are applied to the entire array.

我已经尝试过自己解决问题,但仍然无济于事.请帮忙!

I've tried to work it out myself, but still getting nowhere. Please help!

推荐答案

由于您的值似乎在0到129范围内,因此一种解决方案是将这些值加1(因此它们在1到130的范围内)并使用它们作为替换值向量的索引.然后,您可以使用 CELLFUN 将此操作应用于每个单元.例如:

Since your values appear to span the range 0 to 129, one solution is to add one to these values (so they span the range 1 to 130) and use them as indices into a vector of replacement values. Then you can apply this operation to each cell using the function CELLFUN. For example:

>> C = {0, 129, [8 2 3 4], [6 4], 0};  %# The sample cell array you give above
>> replacement = [9 50 61 61 61 100.*ones(1,125)];  %# A 1-by-130 array of
                                                    %# replacement values (I
                                                    %# added 125 dummy values)
>> C = cellfun(@(v) {replacement(v+1)},C);  %# Perform the replacement
>> C{:}  %# Display the contents of C
ans =
     9

ans =
   100

ans =
   100    61    61    61

ans =
   100    61

ans =
     9

这篇关于在单元格数组中查找并替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆