创建具有所需一个范数的正向量 [英] create a positive vector with desired one norm
问题描述
我想在Matlab中生成一个向量,该向量应为非负数,并且一个范数应等于我可以输入的值(例如1).
I want to generate a vector in Matlab and the vector should be non negative and the one norm should be equal to something which i can input(say 1).
我尝试在线搜索,但无法提出令人满意的建议.
I tried searching online but couldn't come up with satisfactory suggestions.
推荐答案
您可以使用rand
生成随机向量,然后使用norm
来计算向量范数,然后将所有元素相除.然后,您可以将结果乘以输入值,以将其缩放到所需的长度.
You could just generate a random vector using rand
and then use norm
to compute the norm of the vector by which you would divide all elements. You could then multiply the result by your input value to scale it to the desired length.
如果要使用L1范数,请使用以下内容.
If you want the L1 norm, you would use the following.
% This will use the L1 norm
vec = rand(1, 10);
vec = vec * scale ./ norm(vec, 1);
可以使用以下内容将其推广到p
规范:
This could be generalized to the p
norm with the following:
vec = rand(1, 10);
vec = vec * scale ./ norm(vec, p);
这篇关于创建具有所需一个范数的正向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!