replace values of x matrix for positions of y matrix where y=1
1 次查看(过去 30 天)
显示 更早的评论
I have a matrix x that is 12x12double and i have a matrix y also 12x12double.
Its important to note that im running a loop and i will later need to generate bigger sized matrices so i need a code that will work for them as well.
but for now each column of x has these values
col.1
- 3
- 2.85
- 2.55
- 2.25
- 1.95
- 1.65
- 1.35
- 1.05
- 0.75
- 0.45
- 0.15
- 0
y is this
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
i need the values of x to be equal to 1.5 for the positions of y where y = 1. how can i achieve this.
0 个评论
回答(1 个)
Johannes Hougaard
2021-10-19
y = rand(33,33); % This is a y of random numbers between 0 and 1
x = round(rand(33,33)*0.7); % This is another x of random numbers between - rounded to either 1 or 0 as your y
y(x==1) = 1.5; % Assigns all y-values to 1.5 when x equals 1
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!