|
"Michael" <michael.lisowski@gentex.com.extrachar> wrote in message <icgq89$h8b$1@fred.mathworks.com>...
> Hello Everyone,
>
> The following code construct matrix of with 3 col and 10 rows. I want to multiple each element by its corresponding row value. I have done this several ways using a nested four loop, and if statment and the code below. So I in a sense have a solution (3 in fact) that work; however, is there a way to make the code more efficent (if I had a very large matrix) and eliminate the for loop?
>
> vertical = (horzcat(linspace(1,10,10)',linspace(11,20,10)',linspace(21,30,10)'));
> output = zeros(size(vertical));
>
> for loop1=1:numel(vertical)
> [row col] = ind2sub(size(vertical), loop1);
> output(loop1) = vertical(loop1)*row;
>
> end
I.e. this?
output = bsxfun(@times,vertical,(1:size(vertical,1)).');
-Sean
|