How do I make this 2 dimensional vector follow a formula
1 回表示 (過去 30 日間)
古いコメントを表示
I want elements in a 2 dimensional vector two to follow a formula given its location (x,y) in the vector. For example, I want the formula to be like: "element" = 6xy+x+1. So if I looked at the 3rd position across and the 2 element down then it would say 40 because 6(3)(2)+3+1=40. I know that I can do this using 2 for loops but I was hoping for a more efficient way to do it.
0 件のコメント
採用された回答
David Goodmanson
2017 年 1 月 16 日
編集済み: David Goodmanson
2017 年 1 月 16 日
Hello Jesse, The standard way to do this is with the meshgrid function:
x = 1:10; % or whatever size it is
y = 1:10; % same comment
[xx yy] = meshgrid(x,y);
z = 6*xx.*yy+xx+1;
You have to use .* (dot multiply) in the appropriate places so the multiplication is term-by-term. In your case it looks like you want the x and y vectors to just be consecutive integers but in general x and y could have any values and do not even have to be sorted.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!