Vectorized method to filling in array based on logical array
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Noahyt
am 4 Aug. 2016
Kommentiert: Noahyt
am 4 Aug. 2016
I am trying to fill in a new array using values that I have stored in an input vector (sort of like reformat, but more general).
I have a logical array that has true values at locations that correspond to where I want to place one of my input vector values. ONce I have used a value, I want to discard it and use the next value in the vector for the next "open" spot.
I realize that might not have been totally clear, so for example:
I have the input vector and input array
in_vec = [1,2,3,4,5];
in_arr = [0,0,0,0,0; 0,1,1,1,0; 0,0,1,1,0]
and I would like to get the output
out_arr = [0,0,0,0,0; 0,1,2,3,0; 0,0,4,5,0]
I know that I can do this using a for loop that iterates over every item in the logical array and inputting items in the target array, I'm wondering whether there is a smarter way to do this using a vectorized (or other) method?
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 4 Aug. 2016
Bearbeitet: Azzi Abdelmalek
am 4 Aug. 2016
Edit
in_vec = [1,2,3,4,5]
in_arr = [0,0,0,0,0; 0,1,1,1,0; 0,0,1,1,0]
out_arr =zeros(size(in_arr'))
out_arr(logical(in_arr'))=in_vec
out_arr=out_arr'
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!