How to write comments defining the elements in a 2d array?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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
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
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!