How to write comments defining the elements in a 2d array?

조회 수: 1 (최근 30일)
catarina
catarina 2012년 1월 4일
Hello,
I want to write text defining the elements of my matrice but can't figure out how to do it. I need to write a matrice that has element 1 if An <= Yj <= An+1 ; 0 otherwise.
I don't know how to put these instruction about the elements into the matrice. Hope I'm making sense!
Anyone that can help?
Thanks
Catarina

답변 (1개)

Dr. Seis
Dr. Seis 2012년 1월 4일
>> Yj = rand(3,4)
Yj =
0.5447 0.4239 0.1753 0.2433
0.2167 0.8710 0.1654 0.1209
0.8463 0.2685 0.3614 0.5910
>> Zj = (Yj>=0.3).*(Yj<=0.7)
Zj =
1 1 0 0
0 0 0 0
0 0 1 1
Note the ".*" used for element-by-element multiplication. Just substitute the 0.3 and 0.7 for your values associated with An and An+1.
  댓글 수: 3
Dr. Seis
Dr. Seis 2012년 1월 5일
It should work. I just showed an example of Yj (since I don't know what those values actually are), but as long as you substitute "An" and "An+1" for "0.3" and "0.7", respectively, then you should get the correct result.
Fnj = (An<=Yj).*(Yj<=(An+1))
You did want Fnj to be the same sized matrix as Yj, right?
Dr. Seis
Dr. Seis 2012년 1월 5일
Also, you can copy my example above and paste into your Matlab command window to see how things work. For example:
A = (Yj>=0.3)
will produce a logical matrix the same size as Yj, but will have values of 1 for elements of Yj that are >= to 0.3 and values of 0 otherwise.
B = (Yj<=0.7)
will produce a logical matrix the same size as Yj, but will have values of 1 for elements of Yj that are <= 0.7 and values of 0 otherwise. By multiplying each element in A by the corresponding element in B you essentially get the intersection of A and B (i.e., where A and B both equal 1).

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by