using a nested for loop to edit a structure

조회 수: 5 (최근 30일)
Elissa Moller
Elissa Moller 2020년 5월 29일
답변: Arthur Goldsipe 2020년 6월 1일
I am trying to edit the tempFactor field in a PDBStruct. I want to use row 1 of matrix4 to search the AtomSerNo field and when they are equal replace the corresponding tempFactor with row 2 of matrix4. This is the code I tried
for i3=1:length(matrix3)
for i4=1:length(matrix4)
if PDBStruct.Model.Atom.AtomSerNo(i3)==matrix4(1,i4)
PDBStruct.Model.Atom.tempFactor=matrix4(2,i4);
end
end
end
This is the error I got "Expected one output from a curly brace or dot indexing expression, but there were 30471 results"
I think I just dont understand how to properly search within the structure. This format worked great when I was editing a matrix rather than a structure earlier in my code (example below)
for i1=1:size(matrix1(:,1))
for i2=1:size(matrix2(:,1))
if matrix1(i1,1)==matrix2(i2,1)
matrix1(i1,3)=matrix2(i2,2);
end
end
end
  댓글 수: 2
Elissa Moller
Elissa Moller 2020년 5월 30일
this works but it runs for a really long time
for i3=1:length(matrix3)
for i4=1:length(matrix4)
if PDBStructmodel.Model.Atom(i3).AtomSerNo==matrix4(1,i4)
PDBStructmodel.Model.Atom(i3).tempFactor=matrix4(2,i4);
end
end
end
Any suggestions?
Stephen23
Stephen23 2020년 5월 30일
As an alternative to nested loops you could use both outputs of ismember:
for which you will also need to concatenate those values into vectors:

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

답변 (1개)

Arthur Goldsipe
Arthur Goldsipe 2020년 6월 1일
As mentioned in the comments, if you want to compare one atom at a time, the proper way to index is Atom(i3).AtomSerNo. But if you want to do the comparison more efficiently, you can "vectorize" the operation to work across all atoms at once using ismember. The way that I would assign values from matrix4 at once requires me to put them all in a temproary cell array. I would write that code something like this:
[serNoIsInMat4, locationInMat4] = ismember([PDBStructmodel.Model.Atom.AtomSerNo], matrix4(1,:));
tempFactorCell = num2cell(matrix4(2,locationInMat4(serNoIsInMat4)));
[PDBStructmodel.Model.Atom(serNoIsInMat4).tempFactor] = tempFactorCell{:};
There might be some mistakes in my code. Since I don't have access to your data, it's hard for me to confirm I wrote things properly. But hopefully this is enough to help you understand how to proceed.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by