How to sum values of two cell arrays based on another array?.
2 次查看(过去 30 天)
显示 更早的评论
Hey all
How to sum values of Add1 with all elements of Add2 based on ConditionArray.
Add1 = {1,1,1,0.6,0.6,0.3; 1.6,1,1,1,0.6,0.6}
Add2{1,1} = {0,0.8,-1,0.2,-0.5}
Add2{1,2} = {0,0,0.8,1,0}
conditionArray = {[2,3,4,5];[1,3,4,5]}
Result will be based on ConditionArray. Like As ConditionArray{1,1} contains [2,3,4,5] which means that we have to take 2nd,3rd,4th and 5th value from Add1 and add up these values with Add2{1,1}. Similarly ConditionArray{2,1} contains [1,3,4,5] which means that we have to take 1st,3rd,4th and 5th value from Add1 and add up these values with Add2{1,2}. Result may look like this:
ResultantArray{1,1} = {1,1.8,-0.4,0.8,-0.2} % {(1+0),(1+0.8),(0.6+(-1)),(0.6+0.2),(0.3+(-0.5))}
ResultantArray{1,2} = {1.6,1,1.8,1.6,0.6} % {(1.6+0),(1+0),(1+0.8),(0.6+1),(0.6+0)}
Thanks in advance
0 个评论
采纳的回答
Birdman
2018-1-8
%conversion
row1Add1=cell2mat(Add1(1,:));
row2Add1=cell2mat(Add1(2,:));
%taking necessary values
row1Add1=row1Add1(cell2mat(conditionArray(1,:)));
row2Add1=row2Add1(cell2mat(conditionArray(2,:)));
%resultant array
ResultantArray{1,1}=[zeros(1,numel(cell2mat(Add2{1,1}))-numel(row1Add1)) row1Add1]+cell2mat(Add2{1,1})
ResultantArray{1,2}=[zeros(1,numel(cell2mat(Add2{1,2}))-numel(row2Add1)) row2Add1]+cell2mat(Add2{1,2})
4 个评论
Birdman
2018-1-8
If you want to include that number as well, then change the index to
[1 3 4 5 6]
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!