Matrix non-exceeding condition
古いコメントを表示
Hello! I have a 1x15 matrix, the code is presented
[xdata,x]=min(Data); % Data 45x350 x=[ 16 16 16 31 16 0 16....]
a=[15 15 15 15 15....] % 1x350
for i = 1:length(x)-1
if x(i)+a(i)>a(i)
ax=Data((x(i)-a(i):x(i)),i) ;
ay=Data((x(i):x(i)+a(i),i) ;
A{i}=ax;
B{i}=ay;
else x(i)+a(i)<a(i) ;
ax=0 ;
ay=0 ;
A{i}=ax;
B{i}=ay;
end
end
%%% x(i)+a(i) 31+15=46
It so happened that in one value x (i) + a (i) exceeds the size of the matrix, can anyone tell me the condition how can I get around this?
4 件のコメント
Adam
2019 年 8 月 6 日
Depends entirely what you want to happen in such a case. In theory it could happen in every position of your vector since you are adding 15 to a value that could be the biggest index in the vector.
You can clip x(i) + a(i) to the numel of x if you wish or you could ignore values outside the range, but we don't know what you want to happen where this occurs.
You will have to explain what your code is meant to do as it doesn't make much sense.
For example:
if x(i)+a(i)>a(i)
is the same as the much simpler:
if x(i) > 0
and since x are matrix indices (returned by min), you know in advance that it's always going to be true. So, what was the point of that?
Also,
else x(i)+a(i)<a(i) ;
is equivalent to:
else
somethingdiscarded = (x(i) + a(i)) < a(i));
which is the same as
else
somethingdiscarded = false;
Again, I'm sure that wasn't the intent.
Lev Mihailov
2019 年 8 月 6 日
編集済み: Lev Mihailov
2019 年 8 月 6 日
Guillaume
2019 年 8 月 6 日
Something is needed
Yes, some much better explanation of what you're trying to do.
Once again,
else x(i)+a(i)<a(i)
ax=0;
is the same as
else
x(i)+a(i)<a(i) %useless statement that will print 0 in the command window
ax=0;
Perhaps, you meant something entirely different:
elseif x(i)+a(i)<a(i)
ax=0;
but until you explain in words what you're trying to do, we can't know for sure.
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で MATLAB についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!