Transposing a column with a line in a matrix [closed] - matrix

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
We have this matrix of 4x4:
a b c d
e f g h
1 2 3 4
5 6 7 8
By transposing the matrix we get:
a e 1 5
b f 2 6
c g 3 7
d h 4 8
My question is:
What matrix do we get by "transposing column 2 with row 4?"
I need to understand the operation in itself, what does it imply/mean? I never thought of "transposing a column with a line".

AFAIK, It means you are to swap column 2 and row 4, instead of column 1 with row 1 and column2 with row 2 etc.
The code is basically the same as a full transposition, except you only have one column/row

Matrix transposition is a mathematical operation in which a matrix's rows become its columns. From a mathematical perspective, there's no real benefit to transposing only one row in a M x N matrix, but the code to transpose one row is not much different than transposing an entire matrix.
The matrix you get after the transposition would be:
a b 1 d
e f 2 h
c g 3 7
5 6 4 8

Related

How to derive the big O of this algorithm? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
This is the algorithm:
for i ← 1 to n by 1 do
for j ← 1 to i by 1 do
for k ← 1 to j by 1 do
x = x + 1
end
end
end
The number of times the inner loop iterate, depends on the outer loops... so how can the time complexity be derived?
This algorithm increases π‘₯ with the 𝑛th tetrahedral number. This is because the loops can be seen as sums over a range:
The outer loop: βˆ‘π‘–π‘›= 1
The middle loop: βˆ‘π‘—π‘–= 1
The inner loop: βˆ‘π‘˜π‘—= 1
The inner statement: runs in constant time, so that counts as 1 operation.
This means the inner statement runs this many times:
βˆ‘π‘–π‘›= 1 ( βˆ‘π‘—π‘–= 1 ( βˆ‘π‘˜π‘—= 1
1 ) )
The inner sum really is 𝑗, so we can write:
βˆ‘π‘–π‘›= 1 ( βˆ‘π‘—π‘–= 1
𝑗 )
We recognise this expression as the tetrahedral number (see formula at above link to Wikipedia)
This double sum is 𝑛(𝑛 + 1)(𝑛 + 2)/6 = ΞΈ(𝑛³)
Another way to see this, is that the loops lead to combinations for 𝑖, 𝑗, and π‘˜ where they appear in non-decreasing order. So the number of times x = x + 1 is executed, corresponds to the number of ways we can select a multiset of three values from the range 1..𝑛, allowing duplicates. This number is "𝑛 multichoose 3", which leads to the same formula.

I was wondering what the conversion would be for this algebra Ο€A,D (R β‹ˆ ΟƒB=8 (S))

The exercise question asks:
Consider a relation R(A, B, C) and S(A, B, D) containing the
following tuples:
A B C A B D
------- -------
6 8 7 5 8 7
6 6 7 6 6 7
7 8 6 6 8 6
What to be produced from the expression
Ο€A,D(Rβ‹ˆΟƒB=8 (S))
And gives the answer as:
A D
----
6 6
Why is this?
I understand that pi is the projection so it will only output tables A and D. First thing I don't understand is why is it not AAD in the new table as there are 2 A's and Second thing is I don't understand what the selection criteria means.
Selection B=8 from S will give
A B D
5 8 7
6 8 6
Join with R will give
A B C D
6 8 7 6
since A=6 and B=8 in R table(1st row) and resulting S table(2nd row)
From the projection you will see the answer
Let's work from the inside out. First, consider ΟƒB=8 (S). This is a selection. We use S as our source but we only allow through tuples that match the B=8 condition. So lets label this new relation T(A, B, D):
A B D
-------
5 8 7
6 8 6
The 6,6,7 tuple didn't get selected since its B value doesn't equal 8.
Now lets consider Rβ‹ˆT. This is a natural join between my above T tuple and R. Natural join is based on all columns with the same name being used to join the relations. We don't get two As or Bs in this result because a) they're always equal, and b) tuple elements are distinguished by name so you cannot have multiple elements with the same name. So, we produce U(A,B,C,D):
A B C D
----------
6 8 7 6
(Because only tuple (6,8,7) from R and (6,8,6) from T have matching A and B values).
Finally, we project to only retain A and D from U. Hopefully I don't have to explain that.

compute The horizontal absolute difference value of a pixel [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
We have the test image with M rows and N columns as
f(x,y), for x∈ [1,M] and y∈ [1,N ]. The horizontal absolute
difference value of a pixel is defined by
D (x, y) = |f (x, y +1) βˆ’ f (x, y βˆ’1)|.
need help in how to implement it in matlab
This will generate same size matrix, that you need:
mat1 = [zeros(2,size(f,2)); f];% adds 2 rows of zeros to begining
mat2 = [f;zeros(2,size(f,2))]; %adds 2 row of zeros to the end
Dd = mat1-mat2;
D = Dd(2:((size(Dd,1)-1)),:);%crop Dd matrix to size(f)
D = abs( f(1:end-1,:) - f(2:end,:) );
check out diff command as well. Note that D has 1 row less than f.
aux = abs(diff(f,[],2));
D = max(aux(:,1:end-1), aux(:,2:end));
For example: given
f = [3 5 6 4
2 5 4 3
8 9 3 1];
the result is
>> D
D =
2 2
3 1
6 6

Integer algorithm: find a mathematical function that fulfills given conditions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Given an integer x in the interval [1 .. 6], I am looking for two mathematical functions y1 and y2 so that:
y1(x) ∈ [1 .. 6], y2(x) ∈ [1 .. 6]
y1(x) β‰  y2(x) β‰  x
y1(x) and y2(x) are integers
I tried y1(x) = 7-x and y2(x) = (1+x)%6 where % is the remainder or modulo operation.
That solution does not work for x=6. I get y1(x) = y2(x) = 1, which does not fulfills the condition 2. Neither for x=3 and x=5.
Does anyone sees a working solution?
You can use for example:
y1=(x % 6) +1
y2=((x+1) % 6) +1
Functions as table:
x y1 y2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 1
6 1 2
Technically, y1=1+((x+1) %6) and y2=(1+(x+2) %6) both satisfy your request.
I guess though you were thinking about something with a unified distribution of some sort (which is usually the motivation for such attempts...).

R crash on write.csv() for a data.table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Referring to the question Crashing R when calling `write.table` on particular data set, I can almost "reliably" crash 64-bit R --vanilla on Windows-64bit by saving a large data.table in one session. When I say almost, once it happened (when demonstrating the crash to a guy in IT!) that I got the message
Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, :
'getCharCE' must be called on a CHARSXP
referenced in the above question.
To crash R I just need to
save(DT, "datatablefile.RData")
and then in another R session which could be --vanilla, I just say...
load("datatablefile.RData")
write.csv(DT, file='datatablefile.csv')
which will then crash after a minute or two. Note in particular that it will NOT crash if I say
load("datatablefile.RData")
library(data.table)
write.csv(DT, file='datatablefile.csv')
When I say something like
library(data.table)
N <- 1000
DT <- data.table(id=1:N, name=sample(letters, N, replace=TRUE))
save(DT, file='dttest.RData')
and then in another session
load('dttest.RData')
write.csv(DT, 'dttest.csv')
I don't get a crash...
There was the suggestion it might be linked to rbindlist(), so
library(data.table)
N <- 10000000
DT1 <- data.table(id=1:N, name=sample(letters, N, replace=TRUE))
DT2 <- data.table(id=1:N, name=sample(letters, N, replace=TRUE))
DT <- rbindlist(list(DT1, DT2))
save(DT, file='dttest.RData')
Note that I have tried this for N <- 10000000, on this 32gb machine and it still works fine...
It has been suggested it might be due to factors?
library(data.table)
N <- 1000
DT1 <- data.table(id=1:N, name=sample(letters, N, replace=TRUE),
code=as.factor(sample(letters[1:5], N, replace=TRUE)))
DT2 <- data.table(id=1:N, name=sample(letters, N, replace=TRUE),
code=as.factor(sample(letters[1:5], N, replace=TRUE)))
DT <- rbindlist(list(DT1, DT2))
save(DT, file='dttest.RData')
str(DT)
Classes β€˜data.table’ and 'data.frame': 20000000 obs. of 3 variables:
$ id : int 1 2 3 4 5 6 7 8 9 10 ...
$ name: chr "v" "u" "t" "z" ...
$ code: Factor w/ 5 levels "a","b","c","d",..: 2 5 4 2 2 1 2 3 2 4 ...
- attr(*, ".internal.selfref")=<externalptr>
Then in the other session
> load('dttest.RData')
> tables()
Error: could not find function "tables"
> str(DT)
Classes β€˜data.table’ and 'data.frame': 20000000 obs. of 3 variables:
$ id : int 1 2 3 4 5 6 7 8 9 10 ...
$ name: chr "v" "u" "t" "z" ...
$ code: Factor w/ 5 levels "a","b","c","d",..: 2 5 4 2 2 1 2 3 2 4 ...
- attr(*, ".internal.selfref")=<externalptr>
> write.csv(DT, 'dttest.csv')
which then works fine...
It seems fine when I write a large data.table which can contain chr, num, Date but seeems to fail when it contains Factors...
Any suggestions as to how I might figure out how to create a reliable demonstration of how to do replicate this? The contents of the tables themselves are highly confidential.
Update
I've just tried doing
setkey(DT,id)
but it didn't cause a crash.

Resources