Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

DGM
DGM am 5 Jan. 2022
Bearbeitet: DGM am 5 Jan. 2022
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by