linearly spacing a nX1 matrix
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
the cyclist
on 20 Aug 2019
Edited: the cyclist
on 20 Aug 2019
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 Comments
the cyclist
on 20 Aug 2019
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]
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!