substracting every row from each other

조회 수: 6 (최근 30일)
Selma Nur Keskin
Selma Nur Keskin 2020년 6월 6일
댓글: Selma Nur Keskin 2020년 6월 7일
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
Selma Nur Keskin
Selma Nur Keskin 2020년 6월 6일
If anybody could make this work in two loops so that ı understand how it works it would be great

댓글을 달려면 로그인하십시오.

채택된 답변

madhan ravi
madhan ravi 2020년 6월 6일
coor - reshape(coor.',1,size(coor,2),[])
  댓글 수: 1
Selma Nur Keskin
Selma Nur Keskin 2020년 6월 6일
I guess this is working but i am such a beginner , what does 463x3x463 double means? when I store the results in a matrix the matrix size looks like that. Thank you very much btw!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
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
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.
Selma Nur Keskin
Selma Nur Keskin 2020년 6월 7일
Thanks a lot you guys, madhan like I said i am a beginner so i don't understand some notations

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by