When I encounter a -10 in CA, I want to add the two immediate neighboring numbers from CB to it. If the -10 is on an edge/end, ignore it.
% My input would be
CA = {[5 -10 2],[6 6 -10],[0 9 -10 8 -10 3]};
CB = {[4 17 2],[6 6 10],[9 3 4 9 7 6]};
% My output would be
CA = {[5 -4 2],[6 6 -10],[0 9 2 8 5 3]};
% For example: the first -10 encountered (between the 5 and 2 in CA)
% would get 6 added to it (from the 4 and 2 in CB)

 採用された回答

Stephen23
Stephen23 2019 年 5 月 2 日
編集済み: Stephen23 2019 年 5 月 2 日

0 投票

CA = {[5,-10,2],[6,6,-10],[0,9,-10,8,-10,3]};
CB = {[4,17,2],[6,6,10],[9,3,4,9,7,6]};
for k = 1:numel(CA)
idx = 1+find(-10==CA{k}(2:end-1));
CA{k}(idx) = CA{k}(idx)+CB{k}(idx-1)+CB{k}(idx+1);
end
Giving:
>> CA{:}
ans =
5 -4 2
ans =
6 6 -10
ans =
0 9 2 8 5 3

4 件のコメント

Austin Sowers
Austin Sowers 2019 年 5 月 2 日
編集済み: Austin Sowers 2019 年 5 月 2 日
Thank you so much! I have been trying to figure this out fo a long time! Youre a life saver :)
Austin Sowers
Austin Sowers 2019 年 5 月 2 日
I have one more question on this topic that takes it a little further...
ONLY when the -10 in CA1 changes to any other number in CA2, I want to add the two immediate neighboring numbers from CB to CA2. Again, if the -10 is on an edge/end, ignore it.
% My input would be
CA1 = {[5 -10 2],[6 6 -10],[0 9 -10 8 -10 3]};
CA2 = {[5 -9 2],[6 6 -10],[0 9 -10 8 -9 3]};
CB = {[4 17 2],[6 6 10],[9 3 4 9 7 6]};
% My output would be
CA2 = {[5 -3 2],[6 6 -10],[0 9 -10 8 6 3]};
% For example: since the first -10 changed to -9 (in CA1 to CA2),
% we would add 6 to the -9 (from the 4 and 2 in CB).
% For example: The second -10 in the original CA2 (between 9 and 8)
% didnt get anything added to it
% because it remained as a -10 from CA1 to CA2.
Stephen23
Stephen23 2019 年 5 月 2 日
編集済み: Stephen23 2019 年 5 月 2 日
for k = 1:numel(CB)
idx = 1+find(CA1{k}(2:end-1)==-10 & CA2{k}(2:end-1)~=-10);
CA2{k}(idx) = CA2{k}(idx)+CB{k}(idx-1)+CB{k}(idx+1);
end
Giving:
>> CA2{:}
ans =
5 -3 2
ans =
6 6 -10
ans =
0 9 -10 8 6 3
Austin Sowers
Austin Sowers 2019 年 5 月 2 日
You have made my entire week! Thank you thank you thank you!!!! I hope you have a great day!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Argument Definitions についてさらに検索

製品

リリース

R2016b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by