add to values in a varying size cell array

I would like to add 15 to the values in CB that match up with the -10s in CA. (i.e. 17, 4, and 7)
CA = {[-10 2],[6 6 8],[0 9 -10 8 -10]};
CB = {[17 2],[6 6 10],[9 3 4 9 7]};
idx = cellfun(@(x)any(x(:)==-10),CA) & not(cellfun(@(x)any(x(:)==-10),CB)); % Not sure if I am on the right track with this.

Réponses (1)

KSSV
KSSV le 1 Mai 2019
CA = {[-10 2],[6 6 8],[0 9 -10 8 -10]};
CB = {[17 2],[6 6 10],[9 3 4 9 7]};
for i = 1:length(CA)
idx = CA{i}==-10 ;
CB{i}(idx) = CB{i}(idx)+15 ;
end

3 commentaires

Austin Sowers
Austin Sowers le 1 Mai 2019
Modifié(e) : Austin Sowers le 1 Mai 2019
CA = {[5 -10 2],[6 6 8],[0 9 -10 8 -10 3]};
CB = {[4 17 2],[6 6 10],[9 3 4 9 7 6]};
CC = {[1 2 4],[4 2 1],[1 7 3 1 2 2]};
for i = 1:length(CA)
idx = CA{i}==-10 ;
CB{i}(idx) = CB{i}(idx)+15 ;
end
Perfect! I changed CA and CB a bit and I also added a CC. I have one more question and it should settle my problem.
I would like to add the absolute difference between the adjacent values (i.e. 1 and 4. 7 and 1. 1 and 2) in CC, to the value in CB that matches up with the -10 in CA.
So the first: [4 17 2], should then become: [4 20 2]. By (abs(1-4)) in CC
I hope its not too confusing, but youd help me tons! Thank you!
KSSV
KSSV le 1 Mai 2019
My day is confused......:(
Austin Sowers
Austin Sowers le 1 Mai 2019
Ok. Sorry about that. Basically, how do I add CC's [1 2 4] ****(4 minus 1)**** to CB's [4 17 2].

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by