Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error converting 1-dimensional Matrix to Vector #3990

Closed
pluskid opened this issue Aug 8, 2013 · 15 comments
Closed

error converting 1-dimensional Matrix to Vector #3990

pluskid opened this issue Aug 8, 2013 · 15 comments

Comments

@pluskid
Copy link
Member

pluskid commented Aug 8, 2013

Normally computation like rand(5,1) + rand(5) is automatically handled smoothly. But sometimes it fails.

function test(a::Matrix{Float64}, b::Vector{Float64}, c::Real)
    b -= c*a
    b
end

type Foo{T}
    foo ::Vector{T}
end

function test2(a, b::Foo, c::Real)
    b.foo -= c*a[2]
    b.foo
end

a = rand(5,1)
b = rand(5)
c = rand()

test(a,b,c)
test2((0, a), Foo(b), c)

For this example, test2 will fail complaining

ERROR: no method convert(Type{Array{Float64,1}},Array{Float64,2})
@JeffBezanson
Copy link
Member

rand(5,1) + rand(5) gives a 5x1 matrix, which is not a Vector. We could allow converting an array to one of fewer dimensions if the trailing dimensions are 1 though. I feel like that may have been discussed before,

@pluskid
Copy link
Member Author

pluskid commented Aug 8, 2013

@JeffBezanson Ah, I see. It's the assignment to the field causing the error instead of computation.

@StefanKarpinski
Copy link
Member

I thought we'd agreed that .+ should be used for this and + should insist that the shapes match exactly.

@JeffBezanson
Copy link
Member

The only possible exception is if there is an embedding of vectors as Nx1 matrices (and so on). It could be pretty inconvenient if we don't fully support this embedding.

@pluskid
Copy link
Member Author

pluskid commented Aug 10, 2013

It's very inconvenient that some functions only work with Array{T,1} and you have a Array{T,2} with one "dummy" dimension, or vise versa. Given that squeeze is deprecated, is there any handy way of converting back and forth between Array{T,1} and Array{T,2} ?

@pluskid
Copy link
Member Author

pluskid commented Aug 10, 2013

And X[:,i] returns Array{T,1} while X[j,:] returns Array{T,2}. Is this a bug or a feature?

@timholy
Copy link
Member

timholy commented Aug 10, 2013

Given that squeeze is deprecated, is there any handy way of converting back and forth between Array{T,1} and Array{T,2}

squeeze() is not deprecated; only the version where you don't explicitly supply which dimensions you want to squeeze is deprecated. You can search past mailing list archives for discussions about how Matlab's squeeze is horribly broken.

But I think vec() is what you're looking for.

And X[:,i] returns Array{T,1} while X[j,:] returns Array{T,2}. Is this a bug or a feature?

Feature, but not without some controversy at the time it was discussed. Trailing dimensions of length 1 are dropped, no others are.

@timholy
Copy link
Member

timholy commented Aug 10, 2013

I should also say that what you're experiences are transitional issues typical for Matlab users (I went through them myself). Once you get used to the Julian way I predict you'll be reluctant to go back.

@pluskid
Copy link
Member Author

pluskid commented Aug 10, 2013

@timholy Oh cool! Thanks for the references!

As for Julia for Matlab (or for R, for numpy, etc.), maybe it would be great to create some side-by-side cheatsheet like this comparing the behaviors, syntax and conventions, or is there one already?

@ViralBShah
Copy link
Member

There isn't one yet.

@timholy
Copy link
Member

timholy commented Aug 10, 2013

That would be truly awesome to have. If you're willing to take a stab at starting one (or even, just taking notes about things that surprise you as you learn Julia), it would be a huge contribution.

@kmsquire
Copy link
Member

http://mathesaurus.sourceforge.net/ is slightly dated, but might be a good starting point, although I have to express total ignorance at how to convert xml/relaxng to a usable document.

@johnmyleswhite
Copy link
Member

I had made a very simple side-by-side function reference comparing Julia/R and Julia+DataFrames/R: https://docs.google.com/spreadsheet/ccc?key=0Ai9cmDERDCGgdDJ6VDQtQjBGWm5LTzh6R0lRNHY1RVE&usp=sharing

It's probably not ideal for your purposes, but it's complete enough that you might find some use in it.

@staticfloat
Copy link
Member

Is there anything actionable here? Looks like this is mostly stylistic differences, so any objections to close?

@JeffBezanson
Copy link
Member

Covered by #3262.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants