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.

Answers (1)

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 Comments

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!
My day is confused......:(
Ok. Sorry about that. Basically, how do I add CC's [1 2 4] ****(4 minus 1)**** to CB's [4 17 2].

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2016b

Asked:

on 1 May 2019

Commented:

on 1 May 2019

Community Treasure Hunt

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

Start Hunting!