Vectorized method to filling in array based on logical array

4 次查看(过去 30 天)
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?

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-8-4
编辑:Azzi Abdelmalek 2016-8-4
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'

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by