Vectorized method to filling in array based on logical array
11 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Azzi Abdelmalek
on 4 Aug 2016
Edited: Azzi Abdelmalek
on 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'
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!