Specific conditions for array elements
显示 更早的评论
Hello! Let's say that i have an array 5x5 filled with numbers from 1 to 20
A=randi(20,5);
How can i compute a new array B of the same size as the A with three conditions:
1. If the element of the array A is smaller than 10, B=9^2
2. If the element of the array A is equal to 10, B=0
3. If the element of the array A is higher than 10, B=20^0.5
Thank you,
Alex
采纳的回答
更多回答(1 个)
Simon Chan
2021-8-22
You may simply combine them together as follows:
B = (A>10)*(9^2)+(A==10)*0+(A<10)*(20^0.5);
类别
在 帮助中心 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!