逐行向矢量应用函数 [英] Applying function to vectors row by row

查看:94
本文介绍了逐行向矢量应用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个函数应用于一个向量,其中对于每个新行都应用了相同的函数,但是一个变量会发生变化。例如,如果我有一个向量 N rows:

  A = [1.2; 1.5; 1.8; 2.3; 2.7; 2.8; 2.9]; 

我想将 n * 0.1 减去从 n =行号的每行。因此,第2行中的 1.5 将被减去 0.2(2 * 0.1) 2.8 将被减去 0.6(0.1 * 6),依此类推。

为了说明我想要一个函数对我的文件说,OK这是行 n ,我想将 n 中的数减去 n 乘以 0.1 。我希望代码能够逐行读取文件,以便最终结果是向每行执行上述操作的向量。我认为需要一个循环?



我确定解决方案很简单,但我不知道如何去做。

解决方案

我相信这应该可以做到这一点:

  A 
1.2000
1.5000
1.8000
2.3000
2.7000
2.8000
2.9000

b = A-(1:数字(A))。* 0.1
b =
1.1000
1.3000
1.5000
1.9000
2.2000
2.2000
2.2000

它会创建一个列向量,其值为 1:numel(A) ,所以 [1; 2; 3 ...] ,然后将这些值中的每一个乘以 0.1 。这个矢量然后从原始矢量中减去。



正如你看到的,有一个点,。',在那里。这里并不是真的有必要,但最好包含它。如果没有点,'会使共轭转置,而不是常规转换。一>。当然,转置将水平矢量转换为垂直矢​​量。



为了满足 Divakar ,他们可以同时使用 bsxfun,permute和reshape 来做晚餐和建房。



如果你想用矩阵来做这件事,你可以使用 repmat meshgrid ,或者更加真棒 bsxfun ,如下所示:

  A = magic(5); 

b = bsxfun(@minus,A,[1:size(A,1)]。'* 0.1)
b =
16.9000 23.9000 0.9000 7.9000 14.9000
22.8000 4.8000 6.8000 13.8000 15.8000
3.7000 5.7000 12.7000 19.7000 21.7000
9.6000 11.6000 18.6000 20.6000 2.6000 $ b $ 10.5000 17.5000 24.5000 1.5000 8.5000
$ / code>

更多维度?合并 bsxfun 置换

I'm trying to apply a function to a vector where for each new row the same function applies but a variable changes. So for example if I have a vector with N rows:

A = [1.2; 1.5; 1.8; 2.3; 2.7; 2.8; 2.9];

I want to subtract n*0.1 away from each row where n = row number. So 1.5 in row 2 would be subtracted by 0.2 (2*0.1), 2.8 in row 6 would be subtracted by 0.6 (0.1*6), and so on.

To clarify I would like a function that says to my file, OK this is row n and I want to subtract the number in row n by n multiplied by 0.1. I would like the code to be able to read the file row by row so the end result is a vector that has done the above to each row. I think a loop would be needed?

I'm sure the solution is simple but I don't know how to do it.

解决方案

I believe this should do the trick:

A
    1.2000
    1.5000
    1.8000
    2.3000
    2.7000
    2.8000
    2.9000

b = A-(1:numel(A)).'*0.1
b =
    1.1000
    1.3000
    1.5000
    1.9000
    2.2000
    2.2000
    2.2000

What it does is it creates a column vector with values 1:numel(A), so [1; 2; 3 ...], then multiplies each of these values by 0.1. This vector is then subtracted from the original vector.

As you see, there is a dot, .', in there. It is not really necessary here, but it's good practice to include it. Without the dot, ' would make a conjugate transpose, in stead of a regular one. The transpose of course, converts the horizontal vector to a vertical one.

To satisfy Divakar, who can make dinner and build a house at the same time using only bsxfun, permute and reshape:

If you wanted to do this with a matrix instead, you could use repmat, or meshgrid, or the much more awesome bsxfun, like this:

A = magic(5);

b = bsxfun(@minus, A, [1:size(A,1)].'*0.1)
b =
   16.9000   23.9000    0.9000    7.9000   14.9000
   22.8000    4.8000    6.8000   13.8000   15.8000
    3.7000    5.7000   12.7000   19.7000   21.7000
    9.6000   11.6000   18.6000   20.6000    2.6000
   10.5000   17.5000   24.5000    1.5000    8.5000

More dimensions? Combine bsxfun and permute.

这篇关于逐行向矢量应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆