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

3 ビュー (過去 30 日間)
Michal Sleszynski
Michal Sleszynski 2021 年 10 月 19 日
コメント済み: Michal Sleszynski 2021 年 10 月 19 日
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.

回答 (1 件)

Johannes Hougaard
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
  1 件のコメント
Michal Sleszynski
Michal Sleszynski 2021 年 10 月 19 日
Thank you seems like i was overthinking it

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by