replace values of x matrix for positions of y matrix where y=1

2 vues (au cours des 30 derniers jours)
Michal Sleszynski
Michal Sleszynski le 19 Oct 2021
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
  1. 3
  2. 2.85
  3. 2.55
  4. 2.25
  5. 1.95
  6. 1.65
  7. 1.35
  8. 1.05
  9. 0.75
  10. 0.45
  11. 0.15
  12. 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.

Réponses (1)

Johannes Hougaard
Johannes Hougaard le 19 Oct 2021
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

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by