MATLAB Answers

Z.khan
0

linearly spacing a nX1 matrix

Asked by Z.khan
on 20 Aug 2019
Latest activity Commented on by Z.khan
on 20 Aug 2019
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

Sign in to comment.

1 Answer

Answer by the cyclist
on 20 Aug 2019
Edited by the cyclist
on 20 Aug 2019
 Accepted Answer

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

Z.khan
on 20 Aug 2019
I am sorry this is so confusing on my part. Your solution is helpful but here is the problem.
I want to have the first row of aMat begin with 25 let's say and end with 0 and so the rest (45-10=35) should be equally spaced such that from 25 to zero everything is equally spaced.
Similarly, I want the second row to begin with 23 let's say and end with zero but all four columns should be linearly spaced.
Many thanks
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]
Z.khan
on 20 Aug 2019
Thank you very much. You have been extremely kind. I will work on adjusting the sum somehow. Thanks a lot for you help.

Sign in to comment.