MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Little Index Problem in filling a cell array iteratively

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
2018 年 12 月 5 日
are you aware that you are filling empty arrays in T?
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

サインイン to comment.

1 件の回答

回答者: 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 件のコメント

For the first instance i thought something like you did just
w=15;
sum=0;
Length=cellfun(@length,Sequence_ref);
Length2=round(Length./w);
for i=1:length(Sequence_ref)
for j=1:20
Z{i,j}=zeros(1,Length(i));
end
end
this is the difficult part:
for i=1:length(Sequence_ref)
for j=1:20
for c=1:w:Length(i)
Z{i,j}(1,c)=count(Sequence_ref{i,1),'M') %It's just an example
end
end
end
So I want each component of my zero vectors to be replaced by the counting of a particular letter just like as I were using a sliding window. I think it's rigth and MATLAB does the job for the first row of cell changing the double vectors inside Z but when it has to do the same for the other cell rows it crashes outputting "Index exceeds etc"
This is what I get
%Index exceeds array bounds.
%Error in prova (line 26)
%Z{1,1}(1,c)=count(Sequence_ref{i,1},'A');
Anyway thank you Jan! I appreciated!
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.
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

サインイン to comment.



Translated by