MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   查找并替换单元格数组中的值 (https://www.labfans.com/bbs/showthread.php?t=26745)

poster 2019-12-14 20:46

查找并替换单元格数组中的值
 
我有一个像这样的单元格数组:[...

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

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

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

这些规则适用于整个阵列。

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



[B]回答:[/B]

由于您的值似乎在0到129的范围内,因此一种解决方案是在这些值上加一个(因此它们在1到130的范围内)并将它们用作替换值向量的索引。然后,您可以使用[URL="http://www.mathworks.com/help/techdoc/ref/cellfun.html"]CELLFUN[/URL]函数将此操作应用于每个单元格。例如:

>> 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

[url=https://stackoverflow.com/questions/5472017]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 20:32

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.