Info

この質問は閉じられています。 編集または回答するには再度開いてください。

index of zero prolem in matlab

1 回表示 (過去 30 日間)
huda nawaf
huda nawaf 2011 年 12 月 29 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
hi, is there solution for index of zero problem. I'm facing this problem in this code I'm working to design code of local sequence alignment(smith_watrman algorithm)
it is very necessary to fill the first column (zero column) and the first row(zero row) with zero value as initial matrix, then fill the other cells of array with values gradually.
how can run this code? is there another way?
for j=1:length(x)
mat(0,j).scor=0;end;
for i=1:length(y)
mat(i,0).scor=0;end;
%%%%fill matrix
for i=1:length(y)
for j=1:length(x)
%%%%%%calculate match score
letter1=x(j-1:j-1);letter2=y(i-1:i-1);
etc....
end;end;end;
thanks

回答 (1 件)

Andrew Newell
Andrew Newell 2011 年 12 月 29 日
You seem to have a structure array. You could initialize it using
mat = repmat(struct('scor',0),length(y),length(x));
See also Loren's blog.
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 12 月 29 日
Yes, that should be effective enough in most cases.
This will, however, use the "same" scalar 0 to initialize all of the locations, with new memory allocated the first time it becomes necessary to write a value at a location. In some cases it can be important to allocate all of the memory first. The vectorized ways of doing that tend to temporarily require about double the memory, though, so a loop can end up being what you should use.
It will not be possible to assign to index 0 -- not unless you create your own OOP class with a subsref and subsassgn method that understands an index of 0. If there was a good reason why that was necessary (instead of just changing the index calculations) then some of the contributions in the File Exchange might help.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by