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