How to generate and calculate with empty values of matrix-elements

PROBLEM:
I want to use logical functions to perform calculations between
matrices and don't know how to generate an empty value NaN.
EXAMPLE:
I use the two matrices below and if the conditions are not satisfied,
the output should be empty:
clear all;
close all;
m=rand(10,5);
n=rand(10,5);
T = zeros(size(m)); % Make another array to fill up...
for i = 1:numel(m)
if m(i)>.5 && n(i)>.5
T(i) = m(i+1)+n(i+1);
else
T(i) = 'NaN';
end
end
However, I obtain the following error:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
I hope someone know the correction of this mistake
Thank you in advance
Emerson

1 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 8 月 28 日
Please format the code as indicated here: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

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

 採用された回答

Paulo Silva
Paulo Silva 2011 年 8 月 28 日

0 投票

T(i) = 'NaN';
is wrong, your arrays have numeric values but you are trying to put one string in the array, you can't mix numeric values with strings on arrays, do this instead:
T(i) = NaN;
See this simple example:
a=[1 2;3 4];a(1)='NaN'; %error because of what I told you
a=[1 2;3 4];a(1)=NaN; %No error

1 件のコメント

Emerson De Souza
Emerson De Souza 2011 年 8 月 28 日
Thank you Paulo,
I learned it. On the other hand, I obtain the problem with the assigning to T(i) (see comments from Oleg).
I'm trying to reformulate the problem.
Thank you for your help
Emerson

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

その他の回答 (1 件)

Oleg Komarov
Oleg Komarov 2011 年 8 月 28 日

0 投票

You can write directly:
NaN
instead of
'NaN'
but my suggestion would be to preallocate NaNs directly and delete the else part:
T = NaN(size(m))
The other problem is that you are assigning to T(i) the next values in m and n, but if i = 50 then the index will exceed the matrix dimensions.
So, what do you want to accomplish here? (you can also avoid the loop)
EDIT replaced & with |:
m = rand(10,5);
n = rand(10,5);
T = [m(2:end,:) + n(2:end,:); NaN(1,size(m,2))];
T(m(1:end-1,:) <= .5 | n(1:end-1,:) <= .5) = NaN;

9 件のコメント

Emerson De Souza
Emerson De Souza 2011 年 8 月 28 日
Yes Oleg,
I made the change and obtained the error.
All what I want to do is to calculate the elements of a new matrix T based on the elements of two old matrices M and N where the number of rows and columns from M, N and T are identical.
One situation is the element Tij calculated from logical function between Mij and Nij where ij for all matrices are the same.
Another situation is the element Tij calculated from logical function between Mi+x,j and Ni+x,j. This means, the columns of Tj, Mj and Nj are the same but the i-ten element of Ti is calculated from logical functions between Mi+x and Ni+x.
I hope this help you to understand better my intention
Thank you
Emerson
Oleg Komarov
Oleg Komarov 2011 年 8 月 28 日
For the first situation:
Tij = f(Mij,Nij) no problem but what is the logical function? Almost sure that with logical indexing the loop can also be avoided.
For the second situation:
Tij = f(M(i+x)j,N(i+x)j) again what is the logical function and the values of x?
Cannot help more than that w/o additional details.
Emerson De Souza
Emerson De Souza 2011 年 8 月 28 日
an example of logical function could be as below:
if m_ij>.5 && n_ij>.5
T_ij = m_(i+1)j+n_(i+1)j;
else
T_ij = NaN;
But I don't know how to build this information in your suggestion above.
Oleg Komarov
Oleg Komarov 2011 年 8 月 28 日
You have to decide what to do with the last row of T, do you want it to be NaN, because obviously it cannot be:
T_last,j = m_(last+1),j + n_(last+1),j
unless T is k by z and m,n are k+1 by z.
Emerson De Souza
Emerson De Souza 2011 年 8 月 28 日
yes, I can let it to be NaN.
Now how the logical function above
can be incorporated in your suggestion
with NaN for the last element of T?
Emerson De Souza
Emerson De Souza 2011 年 8 月 30 日
EDIT in response to the suggestion below:
m = rand(10,5);
n = rand(10,5);
T = [m(2:end,:) + n(2:end,:); NaN(1,size(m,2))];
T(m(1:end-1,:) <= .5 & n(1:end-1,:) <= .5) = NaN;
Oleg, the suggestions is "almost" right. The problem is that if one element from any matrix is smaller 0.5 it will still calculate but the right answer should be NaN. The sum is allowed ONLY IF both elements are larger 0.5. I tried to change to correct the lines but did not work. May be you know it
Thanks
Emerson
Oleg Komarov
Oleg Komarov 2011 年 8 月 30 日
Replace the & with an |.
Emerson De Souza
Emerson De Souza 2011 年 8 月 30 日
I don't know what is wrong, but it is not working consistently for all elements. Sometimes is correct, but not always.
Could you please have a look on it?
Thanks
Emerson
Oleg Komarov
Oleg Komarov 2011 年 8 月 30 日
You have to be careful when checking m and n, if their first element are both bigger than .5 then the first element of T is the sum of the SECOND elements of m and n.
After correcting for the | I do get consistent results. Try to replace in my example:
m = rand(3);
n = rand(3);
You'll be able to visualize and compare more easily.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by