Operations using individual elements in a matrix?

3 ビュー (過去 30 日間)
Kenneth Choi
Kenneth Choi 2019 年 8 月 6 日
コメント済み: Kenneth Choi 2019 年 8 月 6 日
I'm trying to define a new matrix astar using the individual elements from a and X that correspond to the new matrix. MATLAB is showing this error:
Index in position 2 exceeds array bounds (must not exceed 4).
Error in newalg (line 13)
if X(ii,jj) >= 0 || X(ii,jj) == 0
Could anyone help me debug and improve my code?
load('data');
W = waveletfunction(6);
B = W * data;
A = B(1:243,:);
mu = max(A,[],'all');
v = min(A,[],'all');
a = (2.*A - mu - v)/(mu - v);
X = laplacei(243,4);
n = numel(X);
astar = zeros(size(X));
for ii = 1:n
for jj = 1:n
if X(ii,jj) >= 0 || X(ii,jj) == 0
astar(ii,jj) = (1 - 1/(1 + exp(-a(ii,jj)))) * X(ii,jj);
else
astar(ii,jj) = (1/(1 + exp(-a(ii,jj)))) * X(ii,jj);
end
end
end
disp(astar)

採用された回答

Bob Thompson
Bob Thompson 2019 年 8 月 6 日
The problem is because you are looking to index X(ii, jj) but you have defined the bounds of ii and jj as the number of elements in X. To explain more, if X is 4x1 matrix then n = 4, but then when you call ii = 1, jj = 2 in your loops X(1,2) doesn't exist.
I am not entirely sure what you're trying to accomplish, but you might try defining ii and jj as bounded by size of X instead of numel.
for ii = 1:size(X,1)
for jj = 1:size(X,2)
...
end
end
Also, as a side note, you don't need the 'or' portion of your if condition, because it is covered in the first one.
IF X(ii,jj) is greater than or equal to 0 OR X(ii,jj) is equal to 0
  1 件のコメント
Kenneth Choi
Kenneth Choi 2019 年 8 月 6 日
Thanks for your help and tips. As a matter of fact, I just solved the issue by defining ii = rowsize(X) and jj = colsize(X). Cheers!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Data API for C++ についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by