Vectorising setting values of array by reading from other array
1 次查看(过去 30 天)
显示 更早的评论
I have an array which contains some numbers, and I have another array which is initialised to zeros.
arr = [45; 56 ...] %array with numbers
x-arr = zeros(1000,1) %zero-initialised array
I want to set all index value of x to 1 which have a value in arr namely, as shown, 45 and 56 are values contained in arr, so I want:
x-arr[45] = 1
x-arr[56] = 1
and the rest of the values, which are not contained in arr to remain to zero. Is there a vectorised approach to problem besides the obvious for-loop?
0 个评论
采纳的回答
Fangjun Jiang
2011-11-27
x_arr=zeros(1000,1);
arr=[45;56];
x_arr(arr)=1;
You can't use 'x-arr' as it regards '-' as the minus operator.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!