Info

This question is closed. Reopen it to edit or answer.

index of zero prolem in matlab

1 view (last 30 days)
huda nawaf
huda nawaf on 29 Dec 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (1)

Andrew Newell
Andrew Newell on 29 Dec 2011
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 Comment
Walter Roberson
Walter Roberson on 29 Dec 2011
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.

Tags

Community Treasure Hunt

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

Start Hunting!