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 Kommentar

Selma Nur Keskin
Selma Nur Keskin am 6 Jun. 2020
If anybody could make this work in two loops so that ı understand how it works it would be great

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

madhan ravi
madhan ravi am 6 Jun. 2020

0 Stimmen

coor - reshape(coor.',1,size(coor,2),[])

1 Kommentar

Selma Nur Keskin
Selma Nur Keskin am 6 Jun. 2020
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 6 Jun. 2020
Bearbeitet: Image Analyst am 6 Jun. 2020

0 Stimmen

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 Kommentare

Selma Nur Keskin
Selma Nur Keskin am 6 Jun. 2020
thank you very much, but I want to substract every row from each other row 1 - row1, row1-row2... row1-row n for every row
madhan ravi
madhan ravi am 6 Jun. 2020
Selma what do you think my code does ? Just switch the sign in my code.
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 am 7 Jun. 2020
Thanks a lot you guys, madhan like I said i am a beginner so i don't understand some notations

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by