Hello,
There are now three solutions to the OP's problem.
I have timed them and the results depend on the matrix size.
The solution I thought would be better, Enrico's diag(), is in fact the
slowest. As for the other two, Eric's for loop is 50% fastest than the
matrix index for small matrices
> > Eric Bergeron Wed, 8 Aug 2018 12:53:32 +0300 writes:
>
> > You only need one "for loop"
> > for(i in 2:nrow(myMatrix)) {
> >myMatrix[i-1,i-1] = -1
> >myMatrix[i-1,i] = 1
> > }
Or none, with matrix-based array indexing and explicit control of the indices
to prevent overrun i
> Eric Bergeron Wed, 8 Aug 2018 12:53:32 +0300 writes:
> You only need one "for loop"
> for(i in 2:nrow(myMatrix)) {
>myMatrix[i-1,i-1] = -1
>myMatrix[i-1,i] = 1
> }
>
> HTH,
> Eric
and why are you not using Enrico Schumann's even nicer solution
(from August 6) that I had mention
Thanks a lot ! That's it!
Maija
ke 8. elok. 2018 klo 12.53 Eric Berger (ericjber...@gmail.com) kirjoitti:
> You only need one "for loop"
>
> for(i in 2:nrow(myMatrix)) {
>myMatrix[i-1,i-1] = -1
>myMatrix[i-1,i] = 1
> }
>
> HTH,
> Eric
>
>
> On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärv
You only need one "for loop"
for(i in 2:nrow(myMatrix)) {
myMatrix[i-1,i-1] = -1
myMatrix[i-1,i] = 1
}
HTH,
Eric
On Wed, Aug 8, 2018 at 12:40 PM, Maija Sirkjärvi
wrote:
> Thanks!
>
> If I do it like this:
>
> myMatrix <- matrix(0,5,5*2-3)
> print(myMatrix)
> for(i in 2:nrow(myMatrix))
>
Thanks!
If I do it like this:
myMatrix <- matrix(0,5,5*2-3)
print(myMatrix)
for(i in 2:nrow(myMatrix))
for(j in 2:ncol(myMatrix))
myMatrix[i-1,j-1] = -1
myMatrix[i-1,j] = 1
print(myMatrix)
I get the following result:
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] -1 -1 -1 -1 -1
Hello,
If it is not running as you want it, you should say what went wrong.
Post the code that you have tried and the expected output, please.
(In fact, the lack of expected output was the reason why my suggestion
was completely off target.)
Rui Barradas
On 07/08/2018 09:20, Maija Sirkjärvi w
Thanks, but I didn't quite get it. And I don't get it running as it should.
ti 7. elok. 2018 klo 10.47 Martin Maechler (maech...@stat.math.ethz.ch)
kirjoitti:
>
> > Thanks for help!
> > However, changing the index from i to j for the column vector changes the
> > output. I would like the matrix t
> Thanks for help!
> However, changing the index from i to j for the column vector changes the
> output. I would like the matrix to be the following:
> -1 1 0 0 0 0 0
> 0 -1 1 0 0 0 0
> 0 0 -1 1 0 0 0
> .
> etc.
> How to code it?
as Enrico Schumann showed you: Without any loop, a very nic
Thanks for help!
However, changing the index from i to j for the column vector changes the
output. I would like the matrix to be the following:
-1 1 0 0 0 0 0
0 -1 1 0 0 0 0
0 0 -1 1 0 0 0
.
etc.
How to code it?
Best,
Maija
>> myMatrix <- matrix(0,5,12)
>> for(i in 1:nrow(myMatrix)) {
>>
Hello,
Eric is right but...
You have two assignments. The second sets a value that will be
overwritten is the next iteration by myMatrix[i,i] = -1 when 'i' becomes
the next value.
If you fix the second index and use 'j', you might as well do
myMatrix[] = -1
myMatrix[, ncol(myMatrix)] = 1
H
Quoting Maija Sirkjärvi :
I have a basic for loop with a simple matrix. The code is doing what it is
supposed to do, but I'm still wondering the error "subscript out of
bounds". What would be a smoother way to code such a basic for loop?
myMatrix <- matrix(0,5,12)
for(i in 1:nrow(myMatrix)) {
Both loops are on 'i', which is a bad idea. :-)
Also myMatrix[i,i+1] will be out-of-bounds if i = ncol(myMatrix)
On Mon, Aug 6, 2018 at 12:02 PM, Maija Sirkjärvi
wrote:
> I have a basic for loop with a simple matrix. The code is doing what it is
> supposed to do, but I'm still wondering the err
I have a basic for loop with a simple matrix. The code is doing what it is
supposed to do, but I'm still wondering the error "subscript out of
bounds". What would be a smoother way to code such a basic for loop?
myMatrix <- matrix(0,5,12)
for(i in 1:nrow(myMatrix)) {
for(i in 1:ncol(myMatrix)) {
14 matches
Mail list logo