substracting every row from each other
1 次查看(过去 30 天)
显示 更早的评论
I am trying to substract every other row using 2 loops, what is wrong with this code?
Here is the code,
% these are the coordinates I am using
coor=[x' y' z'];
%number of the rows is my atomnumber
atomnum=size(x,2);
contactmapdistance=zeros;
for i=atomnum : 1
for j= 1:atomnum
contactmapdistance(i,3)=coor(i,:) - coor(j,:);
i=i-1;
end
j=j+1;
end
采纳的回答
更多回答(1 个)
Image Analyst
2020-6-6
编辑:Image Analyst
2020-6-6
Do you mean like this:
% Initialize data
% numRows = 5
x = randi(9, numRows, 1);
y = randi(9, numRows, 1);
z = randi(9, numRows, 1);
% These are the coordinates I am using
coor = [x(:), y(:), z(:)]
% number of the rows is my atomnumber
% atomnum = size(x, 2)
contactMapDistance = zeros(ceil(numRows/2) - 1, 3);
% Subtract row 1 from row 3, row 3 from row 5, row 5 from row 7, etc.
cRow = 1;
for row = 1 : 2 : numRows - 2
contactMapDistance(cRow, :) = coor(row+2, :) - coor(row, :);
cRow = cRow + 1;
end
contactMapDistance % Show in command window
4 个评论
Image Analyst
2020-6-6
Like this:
coor = [x(:), y(:), z(:)]
contactMapDistance = coor(1, :) - coor
Note that the way I set up coor is better than your way because my way can handle it no matter if x, y, and z are row vectors or column vectors, which is not true if you use the ' like you did.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!