Merge selected column elements from two matrices into a new matrix
4 views (last 30 days)
Show older comments
Robert Demyanovich
on 3 Jan 2022
Edited: Robert Demyanovich
on 3 Jan 2022
I have two identically sized matrices. Once matrix, cA, contains the concentration of hydrogen ion while cB contains the concentrations of hydroxide ion. In a specified row, both matrices will have some zero values. For a specific row number, I would like to take the non-zero values of cA and input them into the cA column numbers (which are non zero) in a 3rd matrix, cT. I would also like to take the non-zero values of cB and input them into the cB column numbers (which are non zero) of the identically numbered row for cT. So in row 17 of cA and cB, cA contains non-zero values in columns 1 through 104 whereas cB contains non-zero values in columns 105 through 201. There is never overlap, which means that the row in the new matrix, cT, should not contain any zero values. Also, when the element or cell value for cA is input into cT, I would like to tranform it from hydrogen ion to hydroxide ion using the dissociation constant of water. So the value from cA would be input into cT as follows:
cT(i,j) = 1E-14/cA(i,j)
So, I guess, for a specific row number i, concatenate non-zero values from cA with non-zero values for CB and input that "concatenation" into row i of cT while transforming the cA values as I've outlined above.
0 Comments
Accepted Answer
David Hill
on 3 Jan 2022
If the number of non-zeros is not constant in the combined cA,cB rows, then cT will need to be a cell array.
for k=1:size(cA,1)
A=cA(k,:);
B=cB(k,:);
cT{k}=[A(A~=0),B(B~=0)];
end
otherwise, if the non-zeros is constant, then a new matrix can be formed
for k=1:size(cA,1)
A=cA(k,:);
B=cB(k,:);
cT(k,:)=[A(A~=0),B(B~=0)];
end
1 Comment
More Answers (0)
See Also
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!