How to doulbe all the value on a matrix

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

How to doulbe all the value on a matrix

ADias
Hi,

Is there an expression to double the values of a matrix - without using a loop?
 
What I need is this:

Suppose we have this matrix

> m
     [,1] [,2] [,3]
[1,]    7   17    4  
[2,]   11   10   18    
[3,]   15   19   18  

and I want this matrix

     [,1] [,2] [,3]
[1,]  112  102  115
[2,]  108  109  101
[3,]  104  100  101

where for instance, m[1,1] was obtained by adding (7+17+4+11+10+18+15+19+18)-7

with this loop I am able to get the result I need but I wanted to know if there is a more R way of doing this.

> a<-matrix(c(7,17,4,11,10,18,15,19,18),3,3,T)
> m=a
> for(i in 1:9){
+ m[c(i)]<-sum(a)-a[c(i)]
+ }
> m


thanks
AD
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

Pete Brecknock
If I have understood your question correctly, how about the following ...

m = matrix(c(7,11,15,17,10,19,4,18,18), nrow = 3, ncol=3)

sum_m = sum(m)

new_m = summ-m

HTH

Pete
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

Pete Brecknock
typo ...

should have been

m = matrix(c(7,11,15,17,10,19,4,18,18), nrow = 3, ncol=3)

sum_m = sum(m)

new_m = sum_m-m
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

ADias
Hi,

yes it works perfectly.

I have another question:

Is there way of selecting with a vector the values I wish to take out from a matrix.

Example:

I have this matrix and I want to take out the numbers in bold and get the second matrix below

>m
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   17    1    6    5   19    2   19    1   15     8
 [2,]    7    7   20    3    2   16    9    9   19    13
 [3,]    2    4    3   11   18   11   14   13    3     1
 [4,]    3    7    5    7   17   18   10    6    5    15
 [5,]    8   20   13   10    8   12   20   19    1    16
 [6,]    9   14    1   12   12   12   17   18   10    17
 [7,]    3   10   11    2   12    9   18    6   19     9
 [8,]   13    2   17   16   18    8    9   14    9    16
 [9,]    9    4   11    4    1   17    9    7   20    12
[10,]    9    1    4    8    8   19   19    8   17    18

     [,1] [,2] [,3] [,4]
[1,]    7    3   16    9
[2,]    3    2    9   18
[3,]   13   16    8    9

thanks
AD
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

Dieter Menne
In reply to this post by ADias
ADias wrote
Is there an expression to double the values of a matrix - without using a loop?
 
Why so complicated?
Dieter

> m = matrix(rep(1,20),nrow=4)
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    1    1    1    1    1
[3,]    1    1    1    1    1
[4,]    1    1    1    1    1
> m*3
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    3    3    3    3
[2,]    3    3    3    3    3
[3,]    3    3    3    3    3
[4,]    3    3    3    3    3
>
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

Pete Brecknock
In reply to this post by ADias
try ...

new_m = m[c(2,7,8),c(1,4,6,7)]

HTH

Pete
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

ADias
Pete Brecknock wrote
try ...

new_m = m[c(2,7,8),c(1,4,6,7)]

HTH

Pete
Hi Pete,

I haven't understood what you wanted to say here. Can you explain please?

thanks
ADias
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

Pete Brecknock
I believe you want to select a subset of rows and subset of columns of your original matrix m.

If you had wanted only the first row of m, you could have used m[1,]

Alternatively, if you had wanted only the second column of m then you could have used m[,2]

m[1,2] would give you the element at row 1, column 2.

You are requesting rows 2,7, and 8 and columns 1,4,6 and 7.

The syntax is m[required rows, required columns]

c() allows you to specify multiple rows/columns at the same time.    

HTH

Pete
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

David Winsemius
In reply to this post by ADias

On Jan 17, 2011, at 11:16 AM, ADias wrote:

>
> Hi,
>
> yes it works perfectly.
>
> I have another question:
>
> Is there way of selecting with a vector the values I wish to take  
> out from a
> matrix.
>
> Example:
>
> I have this matrix and I want to take out the numbers in bold and  
> get the
> second matrix below

This is a plain text mailing list (despite what the Nabble mirror may  
(mis-)lead you into believing) ... no bold.

--
david.

>
>> m
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,]   17    1    6    5   19    2   19    1   15     8
> [2,]    7    7   20    3    2   16    9    9   19    13
> [3,]    2    4    3   11   18   11   14   13    3     1
> [4,]    3    7    5    7   17   18   10    6    5    15
> [5,]    8   20   13   10    8   12   20   19    1    16
> [6,]    9   14    1   12   12   12   17   18   10    17
> [7,]    3   10   11    2   12    9   18    6   19     9
> [8,]   13    2   17   16   18    8    9   14    9    16
> [9,]    9    4   11    4    1   17    9    7   20    12
> [10,]    9    1    4    8    8   19   19    8   17    18
>
>     [,1] [,2] [,3] [,4]
> [1,]    7    3   16    9
> [2,]    3    2    9   18
> [3,]   13   16    8    9
>
> thanks
> AD
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-doulbe-all-the-value-on-a-matrix-tp3221213p3221230.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

ADias
OK!!

So, the ideia is from the 1st matrix get the 2nd matrix with the use of a
vector.

is it possible?

In the example I have a 10x10 matrix and I get from that one a second 4x3
matrix selected from a vector.

thanks
ADias


2011/1/17 David Winsemius <[hidden email]>

>
> On Jan 17, 2011, at 11:16 AM, ADias wrote:
>
>
>> Hi,
>>
>> yes it works perfectly.
>>
>> I have another question:
>>
>> Is there way of selecting with a vector the values I wish to take out from
>> a
>> matrix.
>>
>> Example:
>>
>> I have this matrix and I want to take out the numbers in bold and get the
>> second matrix below
>>
>
> This is a plain text mailing list (despite what the Nabble mirror may
> (mis-)lead you into believing) ... no bold.
>
> --
> david.
>
>>
>>  m
>>>
>>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>> [1,]   17    1    6    5   19    2   19    1   15     8
>> [2,]    7    7   20    3    2   16    9    9   19    13
>> [3,]    2    4    3   11   18   11   14   13    3     1
>> [4,]    3    7    5    7   17   18   10    6    5    15
>> [5,]    8   20   13   10    8   12   20   19    1    16
>> [6,]    9   14    1   12   12   12   17   18   10    17
>> [7,]    3   10   11    2   12    9   18    6   19     9
>> [8,]   13    2   17   16   18    8    9   14    9    16
>> [9,]    9    4   11    4    1   17    9    7   20    12
>> [10,]    9    1    4    8    8   19   19    8   17    18
>>
>>    [,1] [,2] [,3] [,4]
>> [1,]    7    3   16    9
>> [2,]    3    2    9   18
>> [3,]   13   16    8    9
>>
>> thanks
>> AD
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/How-to-doulbe-all-the-value-on-a-matrix-tp3221213p3221230.html
>>
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> David Winsemius, MD
> West Hartford, CT
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to doulbe all the value on a matrix

David Winsemius
  You have the capability of using the Nabble interface to post plain  
text. I have checked. There is a little button above your composition  
frame that lets you change to plain text.


On Jan 17, 2011, at 4:38 PM, André Dias wrote:

> OK!!
>
> So, the ideia is from the 1st matrix get the 2nd matrix with the use  
> of a vector.
>
> is it possible?


Peter B already gave you an answer.

 > m[c(2,7, 8), c(1,4,6, 7)]
      V1 V4 V6 V7
[1,]  7  3 16  9
[2,]  3  2  9 18
[3,] 13 16  8  9


>
> In the example I have a 10x10 matrix and I get from that one a  
> second 4x3 matrix selected from a vector.

Then you should have presented the logical conditions for selecting  
that matrix rather than expecting us to guess what they might be.  
Please (re?)read the Posting Guide about what is expected of  
questioners regarding presenting complete examples of data and code.

--
David.

>
> thanks
> ADias
>
>
> 2011/1/17 David Winsemius <[hidden email]>
>
> On Jan 17, 2011, at 11:16 AM, ADias wrote:
>
>
> Hi,
>
> yes it works perfectly.
>
> I have another question:
>
> Is there way of selecting with a vector the values I wish to take  
> out from a
> matrix.
>
> Example:
>
> I have this matrix and I want to take out the numbers in bold and  
> get the
> second matrix below
>
> This is a plain text mailing list (despite what the Nabble mirror  
> may (mis-)lead you into believing) ... no bold.
>
> --
> david.
>
> m
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,]   17    1    6    5   19    2   19    1   15     8
> [2,]    7    7   20    3    2   16    9    9   19    13
> [3,]    2    4    3   11   18   11   14   13    3     1
> [4,]    3    7    5    7   17   18   10    6    5    15
> [5,]    8   20   13   10    8   12   20   19    1    16
> [6,]    9   14    1   12   12   12   17   18   10    17
> [7,]    3   10   11    2   12    9   18    6   19     9
> [8,]   13    2   17   16   18    8    9   14    9    16
> [9,]    9    4   11    4    1   17    9    7   20    12
> [10,]    9    1    4    8    8   19   19    8   17    18
>
>    [,1] [,2] [,3] [,4]
> [1,]    7    3   16    9
> [2,]    3    2    9   18
> [3,]   13   16    8    9
>
> thanks
> AD
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-doulbe-all-the-value-on-a-matrix-tp3221213p3221230.html
>
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> West Hartford, CT
>
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.