From: AP on
Sorry for this elementary question but I couldn't seem to find the answer anywhere....
I have a matrix for which I have changed the nth column by typing B(:;n) = [x; x; x] but I wanted to store it as another matrix G. So something like

G = B(:;n) = [x; x; x]

except the above doesn't work.
From: Frédéric Bergeron on
"AP " <apb1508138(a)gmail.com> wrote in message <i1f87b$smc$1(a)fred.mathworks.com>...
> Sorry for this elementary question but I couldn't seem to find the answer anywhere....
> I have a matrix for which I have changed the nth column by typing B(:;n) = [x; x; x] but I wanted to store it as another matrix G. So something like
>
> G = B(:;n) = [x; x; x]
>
> except the above doesn't work.

One of the solutions:

G=[B(:,1:n-1) [x;x;x] B(:,n+1:end)];

Fred
From: dpb on
Frédéric Bergeron wrote:
> "AP " <apb1508138(a)gmail.com> wrote in message
> <i1f87b$smc$1(a)fred.mathworks.com>...
>> Sorry for this elementary question but I couldn't seem to find the
>> answer anywhere....
>> I have a matrix for which I have changed the nth column by typing
>> B(:;n) = [x; x; x] but I wanted to store it as another matrix G. So
>> something like
>>
>> G = B(:;n) = [x; x; x]
>> except the above doesn't work.
>
> One of the solutions:
>
> G=[B(:,1:n-1) [x;x;x] B(:,n+1:end)];

Or, of course, simply copy and then modify G is another.

Not sure whether there would be more overhead in the catenation above or
the modification of the new after the copy. Wouldn't matter unless in a
loop or the sizes were very large indeed.

--
From: Frédéric Bergeron on
> Not sure whether there would be more overhead in the catenation above or
> the modification of the new after the copy. Wouldn't matter unless in a
> loop or the sizes were very large indeed.

If the caculating time is to minimize, forvery large matrix:

B=rand(2000,2000);
n=300;
x=1;
replacing_matrix=x*ones(size(B,1),1);

%First method: concatenation
tic;
G1=[B(:,1:n-1) replacing_matrix B(:,n+1:end)];
t1=toc;

%Second method: copy then modify
tic;
G2=B;
G2(:,n)=replacing_matrix;
t2=toc;

fprintf('\nTime for concatenation: %g\nTime for copy then modify: %g\n',t1,t2);
% Returns:
% Time for concatenation: 0.430226
% Time for copy then modify: 0.124931

It's in the same order, but the copy-then-modify time is in general less than the concatenation time. I've tried some value for the size of B.
From: James Tursa on
"Frédéric Bergeron" <frederic.bergeron(a)logiag.com> wrote in message <i1fb53$eqo$1(a)fred.mathworks.com>...
> > Not sure whether there would be more overhead in the catenation above or
> > the modification of the new after the copy. Wouldn't matter unless in a
> > loop or the sizes were very large indeed.
>
> If the caculating time is to minimize, forvery large matrix:
>
> B=rand(2000,2000);
> n=300;
> x=1;
> replacing_matrix=x*ones(size(B,1),1);
>
> %First method: concatenation
> tic;
> G1=[B(:,1:n-1) replacing_matrix B(:,n+1:end)];
> t1=toc;
>
> %Second method: copy then modify
> tic;
> G2=B;
> G2(:,n)=replacing_matrix;
> t2=toc;
>
> fprintf('\nTime for concatenation: %g\nTime for copy then modify: %g\n',t1,t2);
> % Returns:
> % Time for concatenation: 0.430226
> % Time for copy then modify: 0.124931
>
> It's in the same order, but the copy-then-modify time is in general less than the concatenation time. I've tried some value for the size of B.

That's because the concatenation method copies the data twice. Once to form the B(:,1,n-1) and B(:,n+1:end) matrices, and again to form the concatenated result. That is not efficient. Best is to make a shared data copy first G2 = B like your second example because no data copy is involved. Then when you do the column replacement the data is copied, once, and then modified.

James Tursa
 | 
Pages: 1
Prev: Removing columns
Next: Polygon