# Replace cell array based on condition

3 vues (au cours des 30 derniers jours)
Song JL le 2 Fév 2020
Commenté : Song JL le 3 Fév 2020
i have a problem in replacing cell array based on condition
if i have
A = {[1,1,2; 2,2,3];[1,3,4; 9,6,8];[1,2,5; 2,3,4];[1,1,4; 3,4,5]};
B = [2,2,2; 3,3,3];
then, the condition that apply is when A has a value more than 5 it should be replace with B
so, the result have to be like this
result = {[1,1,2; 2,2,3];[2,2,2; 3,3,3];[1,2,5; 2,3,4];[1,1,4; 3,4,5]};
is it possible to do that ?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Ioannis Andreou le 2 Fév 2020
Use cellfun:
tfReplace = cellfun(@(c) any(any(c>5)), A, 'UniformOutput', true);
A{tfReplace} = B;
##### 2 commentairesAfficher AucuneMasquer Aucune
Song JL le 2 Fév 2020
It works!! Thank you so much !!
Song JL le 3 Fév 2020
Hi, what if i have more than one array that need to be replaced??
for example:
A = {[1,1,2; 2,2,3];[1,3,4; 9,6,8];[1,7,8; 2,3,4];[1,1,4; 8,6,5]};
and then the result should be
result = {[1,1,2; 2,2,3];[2,2,2; 3,3,3];[2,2,2; 3,3,3];[2,2,2; 3,3,3]};
i have tried to solve it using for , but i still get an error
here is my code
for r = 1:length(tfReplace)
A{tfReplace(r)} = B;
end
seems that it isn't appropriate way..

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by