Merge selected column elements from two matrices into a new matrix
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
David Hill
2022년 1월 3일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!