Find and replace values ​​in an array of cells

I have an array of cells similar to this: [...

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

I just want to find and replace certain values, but I cannot use a regular function because the cells have different lengths. I need to replace many specific values ​​at the same time and there is no general function on how the values ​​are replaced. However, sometimes several input values ​​must be replaced by the same output.

so I want to say that 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 apply to the entire array.

I tried to figure it out myself, but still I get nothing. Please, help!

+4
source share
1 answer

Since your values ​​appear to cover a range from 0 to 129, one of them is to add one of these values ​​(therefore they cover a range from 1 to 130) and use them as indices in the vector of replacement values. You can then apply this operation to each cell using the CELLFUN function. For instance:

>> 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 
+3
source

Source: https://habr.com/ru/post/1345701/


All Articles