matrix operations on matrix with "missing" elements

Viewed 89

I'm trying to analyse some experimental in a matrix and I'm having some issues. For example I'd like to scale the columns of a matrix so that the first row of each column is 1. I'd like to do it in the neat/clean julia way that I'm now starting to learn but I'm struggling to find a good solution.

  1. The problem comes from the fact that each column is the result of some experimental test, and they have different lengths. I've "fixed" this by creating the matrix in excel, adding a missing in the empty cells at the bottom of the column and then copy pasting it in julia. I take this is probably not the best way to deal with the issue?

Example: (normal matrices are much bigger though)

A=[1 2 3
   4 5 6
 missing missing 9]
  1. After that, I'd like to do some analysis, one of which is scaling the matrix so that the first row is = [1 1 1...1]. I tried both map map((x,y)->x./y,A[2:end,:],A[1,:]) but it seems to apply the top row the the first N elements of the first column only. Alternatively I tried with mapslices but I'm getting the following error MethodError: Cannot `convert` an object of type Missing to an object of type Float64

I have the feeling I'm missing something and my googlefoo is failing me... any help is much appreciated!

PS: Apologies if I missed some already answered question or if I missed some guideline, I'll try to improve my question if needed. It's the first time I post here!

1 Answers

I'm not sure what your first question is, it seems hard to answer without knowing what the data you're processing it looks like.

Your second question if I understand correctly should be as simple as:

julia> A ./ A[1, :]'
3×3 Matrix{Union{Missing, Float64}}:
 1.0       1.0       1.0
 4.0       2.5       2.0
  missing   missing  3.0

Edit to add:

Whether a matrix is or isn't a good idea here depends on the wider context, but if you have some vectors of numbers of different lengths, you can just put them in a vector of vectors rather than a matrix, which means they don't all have to have the same length:

julia> x = rand(3); y = rand(5);

julia> A = [x, y]
2-element Vector{Vector{Float64}}:
 [0.2654489138174001, 0.8598585826482341, 0.43527866751212607]
 [0.4702376843007643, 0.7890927390349933, 0.6073796489306595, 0.9178238662871376, 0.5917433487576529]

julia> A ./ first.(A)
2-element Vector{Vector{Float64}}:
 [1.0, 3.2392620119731323, 1.6397831931290188]
 [1.0, 1.6780721013637205, 1.2916439264833126, 1.9518296745015802, 1.2583920185758093]
Related