Re: [R] Need help to print matrix with element and position

2021-10-28 Thread Rui Barradas

Hello,

With no loops:


cbind(
  row = c(t(row(mat_1))),
  col = c(t(col(mat_1))),
  mat_1 = as.numeric(t(mat_1))
)


If the matrix entries are not numeric, use cbind.data.frame. This will 
keep the row and column numbers as numbers, the default cbind method 
would coerce them to the class of mat_1's elements.



cbind.data.frame(
  row = c(t(row(mat_1))),
  col = c(t(col(mat_1))),
  mat_1 = c(t(mat_1))
)


Hope this helps,

Rui Barradas


Às 09:19 de 28/10/21, Anas Jamshed escreveu:

I create a matrix of size 3x3 called mat_1 and then I want to iterate over
all the values one by one and print the element as well as the position in
the matrix:

My code is :

mat_1= matrix( c('1','2','3','4','5','6','7','8','9'), nrow = 3, ncol =
3,byrow = TRUE)
mat_1
# Loop over my_matrix
for(row in 1:nrow(mat_1)) {
 for(col in 1:ncol(mat_1)) {
 print(mat_1[row, col])
 }
}

But I don't know how to print elements and positions as well of this matrix?

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Need help to print matrix with element and position

2021-10-28 Thread Jim Lemon
mat_1= matrix( c('1','2','3','4','5','6','7','8','9'), nrow = 3, ncol =
3,byrow = TRUE)
mat_1
# Loop over my_matrix
for(row in 1:nrow(mat_1)) {
for(col in 1:ncol(mat_1)) {
cat(row,col,mat_1[row,col],"\n")
}
}

On Thu, Oct 28, 2021 at 8:01 PM Anas Jamshed  wrote:
>
> where should I place  cat(row,col,mat_1[row,col],"\n")?
>
> On Thu, Oct 28, 2021 at 1:58 PM Jim Lemon  wrote:
>>
>> Hi Anas,
>> How about:
>>
>> cat(row,col,mat_1[row,col],"\n")
>>
>> Jim
>>
>> On Thu, Oct 28, 2021 at 7:19 PM Anas Jamshed  
>> wrote:
>> >
>> > I create a matrix of size 3x3 called mat_1 and then I want to iterate over
>> > all the values one by one and print the element as well as the position in
>> > the matrix:
>> >
>> > My code is :
>> >
>> > mat_1= matrix( c('1','2','3','4','5','6','7','8','9'), nrow = 3, ncol =
>> > 3,byrow = TRUE)
>> > mat_1
>> > # Loop over my_matrix
>> > for(row in 1:nrow(mat_1)) {
>> > for(col in 1:ncol(mat_1)) {
>> > print(mat_1[row, col])
>> > }
>> > }
>> >
>> > But I don't know how to print elements and positions as well of this 
>> > matrix?
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > __
>> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Need help to print matrix with element and position

2021-10-28 Thread Anas Jamshed
where should I place  cat(row,col,mat_1[row,col],"\n")?

On Thu, Oct 28, 2021 at 1:58 PM Jim Lemon  wrote:

> Hi Anas,
> How about:
>
> cat(row,col,mat_1[row,col],"\n")
>
> Jim
>
> On Thu, Oct 28, 2021 at 7:19 PM Anas Jamshed 
> wrote:
> >
> > I create a matrix of size 3x3 called mat_1 and then I want to iterate
> over
> > all the values one by one and print the element as well as the position
> in
> > the matrix:
> >
> > My code is :
> >
> > mat_1= matrix( c('1','2','3','4','5','6','7','8','9'), nrow = 3, ncol =
> > 3,byrow = TRUE)
> > mat_1
> > # Loop over my_matrix
> > for(row in 1:nrow(mat_1)) {
> > for(col in 1:ncol(mat_1)) {
> > print(mat_1[row, col])
> > }
> > }
> >
> > But I don't know how to print elements and positions as well of this
> matrix?
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Need help to print matrix with element and position

2021-10-28 Thread Jim Lemon
Hi Anas,
How about:

cat(row,col,mat_1[row,col],"\n")

Jim

On Thu, Oct 28, 2021 at 7:19 PM Anas Jamshed  wrote:
>
> I create a matrix of size 3x3 called mat_1 and then I want to iterate over
> all the values one by one and print the element as well as the position in
> the matrix:
>
> My code is :
>
> mat_1= matrix( c('1','2','3','4','5','6','7','8','9'), nrow = 3, ncol =
> 3,byrow = TRUE)
> mat_1
> # Loop over my_matrix
> for(row in 1:nrow(mat_1)) {
> for(col in 1:ncol(mat_1)) {
> print(mat_1[row, col])
> }
> }
>
> But I don't know how to print elements and positions as well of this matrix?
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] Need help to print matrix with element and position

2021-10-28 Thread Anas Jamshed
I create a matrix of size 3x3 called mat_1 and then I want to iterate over
all the values one by one and print the element as well as the position in
the matrix:

My code is :

mat_1= matrix( c('1','2','3','4','5','6','7','8','9'), nrow = 3, ncol =
3,byrow = TRUE)
mat_1
# Loop over my_matrix
for(row in 1:nrow(mat_1)) {
for(col in 1:ncol(mat_1)) {
print(mat_1[row, col])
}
}

But I don't know how to print elements and positions as well of this matrix?

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.