フィルターのクリア

How to assign value in an array depending on the index stated in another array

6 ビュー (過去 30 日間)
Hi
I have an array of size 11x2 where the first column is the index value and the second is the value. I would like to create another array of size 24x1 where the values of the indexs mentioned in the first array are one I tried the following but it did not owrk :
schedule= (zeros(size(24)))
schedule(lowestpricesgrid(:,1))=1
The lowest prices grid contains the following values:
index value
6 39
7 40
5 40
4 40
8 41
3 42
2 44
14 44
9 44
10 45
13 45
The code above returns a array of size 1x14 : 0 1 1 1 1 1 1 1 1 1 0 0 1 1
I would like it to ideally be : 0 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0
Thank you for the help!

採用された回答

DGM
DGM 2022 年 1 月 5 日
編集済み: DGM 2022 年 1 月 5 日
The first problem is this:
zeros(size(24)) % this is a scalar
ans = 0
because
size(24)
ans = 1×2
1 1
As a result, the output vector starts out undersize and is only expanded to accomodate the largest index in the list (14).
Allocating it to the correct size will give a full 24 element vector.
lowestpricesgrid = [6 39;
7 40;
5 40;
4 40;
8 41;
3 42;
2 44;
14 44;
9 44;
10 45;
13 45];
schedule = zeros(1,24);
schedule(lowestpricesgrid(:,1)) = 1
schedule = 1×24
0 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0

その他の回答 (0 件)

カテゴリ

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