linearly spacing a nX1 matrix

2 ビュー (過去 30 日間)
Z.khan
Z.khan 2019 年 8 月 20 日
コメント済み: Z.khan 2019 年 8 月 20 日
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?

採用された回答

the cyclist
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
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]
Z.khan
Z.khan 2019 年 8 月 20 日
Thank you very much. You have been extremely kind. I will work on adjusting the sum somehow. Thanks a lot for you help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by