linearly spacing a nX1 matrix
2 ビュー (過去 30 日間)
古いコメントを表示
Hi!
I have an nx1 matrix. I want to linearly divide the value in each row into 16 columns thus giving me a nx16 linearly spaced matrix.
For a little example let us say I have a=[45; 50] now I want to creat a 2x4 matrix such that the values in consecutive clolumns are linearly spaced and their sum is equal to 45. Could someone help on this one please?
0 件のコメント
採用された回答
the cyclist
2019 年 8 月 20 日
編集済み: the cyclist
2019 年 8 月 20 日
Your problem is still under-specified, and an infinite number of matrices will meet you conditions. Here is one possible solution:
a = [45; 50];
numberColumns = 4;
aMat = a + [0:numberColumns-1];
aMat = (aMat - mean(aMat,2) + a)/numberColumns;
8 件のコメント
the cyclist
2019 年 8 月 20 日
So, now you've gone from an under-specified problem to an over-specified one.
If you require ...
- start value
- end value
- number of columns
then you cannot guarantee a particular sum.
This code will get you the listed three:
a = [45; 50];
start = [25; 23];
finish = [ 0; 0];
numberRows = size(a,1);
numberColumns = 4;
output = zeros(numberRows,numberColumns);
for ir = 1:numberRows
output(ir,:) = linspace(start(ir),finish(ir),4);
end
output =
25.0000 16.6667 8.3333 0
23.0000 15.3333 7.6667 0
but sum(output,2) = [50; 46]
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!