Help needed on alternate row and column replacing matrix value

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help needed on alternate row and column replacing matrix value

seltek
I wish to create a matrix that would use the values of initial matrix (for example a 3x3 ones matrix) to set the values in this new matrix (a 6x6 matrix (or 2*Nx2*N))to be equal to the values of the initial matrix on every alternate column and row

For example:
>The first row, first column value of the initial matrix would replace the first row,first column value of the new matrix
>Then the first row, second column value of the initial matrix would replace the first row, third column value of the new matrix

I tried making a do loop to do this but my loop would of course only work for the first column and first row; I would like this to be done for every odd row and column

Here is the loop I used:
N=3;
A=ones(N,N);
N_double=2*N;
C=zeros(N_double,N_double);
i=0;
do
i++;
j=(2*i)-1;
C(1,j)=A(i,1);
C(j,1)=A(1,i);
until(i==N);
C

I am quite new to using octave and any mathematical computing code so any help would be appreciated and i hope i havnt made this sound too confusing as i am struggling to explain this as it is
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on alternate row and column replacing matrix value

Juan Pablo Carbajal-2
On Tue, Dec 16, 2014 at 1:04 AM, seltek <[hidden email]> wrote:

> I wish to create a matrix that would use the values of initial matrix (for
> example a 3x3 ones matrix) to set the values in this new matrix (a 6x6
> matrix (or 2*Nx2*N))to be equal to the values of the initial matrix on every
> alternate column and row
>
> For example:
>>The first row, first column value of the initial matrix would replace the
> first row,first column value of the new matrix
>>Then the first row, second column value of the initial matrix would replace
> the first row, third column value of the new matrix
>
> I tried making a do loop to do this but my loop would of course only work
> for the first column and first row; I would like this to be done for every
> odd row and column
>
> Here is the loop I used:
> N=3;
> A=ones(N,N);
> N_double=2*N;
> C=zeros(N_double,N_double);
> i=0;
> do
> i++;
> j=(2*i)-1;
> C(1,j)=A(i,1);
> C(j,1)=A(1,i);
> until(i==N);
> C
>
> I am quite new to using octave and any mathematical computing code so any
> help would be appreciated and i hope i havnt made this sound too confusing
> as i am struggling to explain this as it is
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Help-needed-on-alternate-row-and-column-replacing-matrix-value-tp4667810.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

Is this what you want?
A = ones (3,3);
B = zeros (6,6);
B(1:2:end,1:2:end) = A;

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on alternate row and column replacing matrix value

seltek
This is not quite what i was looking for as i would like this to work for any value of N where N is the size of my matrix A and matrix B is size 2N and this would not work as the dimension would not match when placed into octave;
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on alternate row and column replacing matrix value

Andreas Weber-4
In reply to this post by seltek
Am 16.12.2014 um 01:04 schrieb seltek:
> I wish to create a matrix that would use the values of initial matrix (for
> example a 3x3 ones matrix) to set the values in this new matrix (a 6x6
> matrix (or 2*Nx2*N))to be equal to the values of the initial matrix on every
> alternate column and row

This?

A = [4 2 7; 8 4 3; 7 9 2]

if (! issquare (A))
  error ("A has to be a suqare matrix")
endif

N = rows (A);
B = zeros (2 * N, 2 * N);
B(1:2:end, 1:2:end) = A

B =

   4   0   2   0   7   0
   0   0   0   0   0   0
   8   0   4   0   3   0
   0   0   0   0   0   0
   7   0   9   0   2   0
   0   0   0   0   0   0

-- Andy

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on alternate row and column replacing matrix value

seltek
Thanks Andy,
This is what i was looking for
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on alternate row and column replacing matrix value

Przemek Klosowski-7
In reply to this post by seltek
On 12/15/2014 07:04 PM, seltek wrote:
I wish to create a matrix that would use the values of initial matrix (for
example a 3x3 ones matrix) to set the values in this new matrix (a 6x6
matrix (or 2*Nx2*N))to be equal to the values of the initial matrix on every
alternate column and row
Octave is very good at operations on entire matrices. You can of course write loops that go element-by-element but it's faster and easier to write 'vectorized' code---you just have to get used to it, and it's definitely worth the effort.

Octave has a range operator that can express your skipped index: 1:2:N, and so your assignment can be written as

new=initial(1:2:N,1:2:N)


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave