I have two matrices a
and b
of 1 column and n rows.
These matrices represent min and max values.
From these I need to create a n by m matrix.
m is the max value of [a ; b]
The result must contain each value between a(:)
and b(:)
, padded with zeros.
note: it's easy to do with a for loop, but I want to avoid loops.
Exemple :
Starting with these two matrices :
>> a = [3 ; 5 ; 2 ; 7 ; 4]
a =
3
5
2
7
4
>> b = [7 ; 7 ; 5 ; 8 ; 4]
b =
5
7
4
8
4
I want to end with this matrix :
result =
3 4 5 6 7 0 0 0
5 6 7 0 0 0 0 0
2 3 4 5 0 0 0 0
7 8 0 0 0 0 0 0
4 0 0 0 0 0 0 0
So far I have this :
>> result = zeros(size(a,1), max([a ; b]));
>> rows = [1:size(a,1)]
rows =
1 2 3 4 5
>> index = sub2ind(size(result), rows, b - a + 1)
>> result(index) = b
result =
0 0 0 0 7 0 0 0
0 0 7 0 0 0 0 0
0 0 0 5 0 0 0 0
0 8 0 0 0 0 0 0
4 0 0 0 0 0 0 0
>> result(:,1) = a
result =
3 0 0 0 7 0 0 0
5 0 7 0 0 0 0 0
2 0 0 5 0 0 0 0
7 8 0 0 0 0 0 0
4 0 0 0 0 0 0 0