Little Index Problem in filling a cell array iteratively

조회 수: 2 (최근 30일)
Bio_Ing_Sapienza
Bio_Ing_Sapienza 2018년 12월 5일
댓글: Bio_Ing_Sapienza 2018년 12월 6일
I'm really new to Matlab so I'm having a certain number of issues that surely may be avoided with experience like this
T=cell(numel(Sequence_ref),20);
w=15;
sum=0;
for i=1:numel(Sequence_ref)
sum=sum+1
for j=1:20
T{i,j}=zeros(1,floor(numel(Sequence_ref{i,1})./w));
end
end
When it has to start the second iteration MATLAB responds saying that "INDEX EXCEEDS ARRAY BOUNDS". Sequence_ref is a cell array containing nucleotide (5515x1) sequences so each i will score along this gigantic column cell array. Anyone can help me to fix it?
  댓글 수: 2
madhan ravi
madhan ravi 2018년 12월 5일
are you aware that you are filling empty arrays in T?
Bio_Ing_Sapienza
Bio_Ing_Sapienza 2018년 12월 5일
Yes I'm aware. I need to fill it with zeros vectors to make possible another step that I need to do. The problem is that I don't understand why MATLAB does not fill my, empty, cell array with zeros vectors as specified

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

답변 (1개)

Jan
Jan 2018년 12월 5일
편집: Jan 2018년 12월 5일
Do not use "sum" as name of a variable, because this causes confusion frequently, when you try to use the function sum() afterwards. Avoid such shadowing in general.
In addition sum is not used inside the loops, so simply omit it.
T = cell(numel(Sequence_ref), 20);
w = 15;
for i = 1:numel(Sequence_ref)
for j = 1:20
width = floor(numel(Sequence_ref{i, 1}) ./ w)
T{i,j} = zeros(1, width);
end
end
width does not depend on the inner loop, so it is more efficient to move it before the loop. You can replace the innerloop also by a vectorized version:
T = cell(numel(Sequence_ref), 20);
w = 15;
for i = 1:numel(Sequence_ref)
width = floor(numel(Sequence_ref{i, 1}) ./ w);
T(i, :) = {zeros(1, width)};
end
You mention, that the cell is filled with empty arrays. Then width is 0. Maybe you want to define it differently, but I cannot know how.
Now the error "INDEX EXCEEDS ARRAY BOUNDS" (prefer to post a copy of the complete message instead of cropping a short part only):
Either Sequence_ref is not a column cell, as you expect, or you have redefined one of the functions as a variable before: numel or floor. Check this by using the debugger:
dbstop if error
Then run the code again until it stops. Now check:
which numel
which floor
size(Sequence_ref)
  댓글 수: 3
Jan
Jan 2018년 12월 6일
편집: Jan 2018년 12월 6일
I mention it every time I see it:
Length = cellfun('length', Sequence_ref);
% Or:
Length = cellfun('prodofsize', Sequence_ref); % This should be called 'numel'
is much faster than using a function handle @length.
I suggested to use the debugger already. You will find out easily, what the problem is, while the readers in the forum have to guess it.
dbstop if error
Then run the code again. Then check the size:
% Failing: Z{1,1}(1,c)=count(Sequence_ref{i,1},'A');
size(Sequence_ref)
i
c
size(Z)
This should reveal the problem already. The debugger is the best friend of the programmer.
By the way: The posted code cannot run due to a misplaced parenthesis:
count(Sequence_ref{i,1),'M')
% ^ ^ One is curly brace, one is round parenthesis
Please post exactly the code you run.
Bio_Ing_Sapienza
Bio_Ing_Sapienza 2018년 12월 6일
w=15;
sum=0;
Length=cellfun('length',Translated_ref);
Length=round(Length./w);
for i=1:length(Translated_ref)
for j=1:20
Z{i,j}=zeros(1,Length(i));
end
end
for i=1:length(Translated_ref)
for c=1:w:Length(i)
Z{i,2}(1,c)=count(Translated_ref{i,1}, 'N'); % I want it to fill the zero vector in the second cell of each i-th row
% with the N occurrences. c=1:w:Length(i) is used to scan the sequence
% 15 letters per time
end
end

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by