Replace value in a matriz

153 views
Skip to first unread message

Rosangela Oliveira

unread,
Oct 6, 2015, 1:43:58 PM10/6/15
to julia-users
Hi,
How to replace the values in a matriz using maxval and minval?
for example, I would change the max value in a line for o(zero) and the max value for one.

I'm trying something like this...
ma=[0.4 0.3; 0.6 0.9;-0.6 15;0.5 0.5]
for i1 = 1:size(ma,1)
max=findmax(ma[i1,:])
min=findmin(ma[i1,:])
end

Thanks for helping.

Mauricio Esteban Cuak

unread,
Oct 6, 2015, 3:48:26 PM10/6/15
to julia-users
Hi,

You didn't tell us what to do with the other numbers, so I'm assuming you can also have two columns per row?

for row=1:size(ma,1)
    ma
[row, 1] = (ma[row, 1] >= ma[row, 2])
   
ma[row, 2] = (ma[row, 1] <  ma[row, 2])
end


gives

4x2 Array{Float64,2}:
 
1.0  0.0
 
0.0  1.0
 
0.0  1.0
 
1.0  0.0

Message has been deleted

Rosangela Oliveira

unread,
Oct 6, 2015, 4:31:57 PM10/6/15
to julia-users
Hi Mauricio,
The idea is to read each matrix nxm and  to replace the max value=1 and  min value=0 in case when max and min its the same value, the first receive 1 and the second recive 0.

Thans!:)

Steven G. Johnson

unread,
Oct 6, 2015, 4:32:15 PM10/6/15
to julia-users
On Tuesday, October 6, 2015 at 4:27:40 PM UTC-4, Rosangela Oliveira wrote:
The idea is to read each matrix nxm and  to replace the max value=1 and  min value=0 in case when max and min its the same value, the first receive 1 and the second recive 0.
 
Just write loops.  It will be way less convoluted, and way faster, than any construction using find etc.

With Matlab and Python, every programming problem is an exercise in mining the standard library for (you hope) just the right functions for your problem, because any code that you write yourself will be slow.  Julia doesn't have that problem, and often the most straightforward code is also the best code.

Mauricio Esteban Cuak

unread,
Oct 6, 2015, 10:29:02 PM10/6/15
to julia-users
How about this:

function change_min_max!{T}(m::Array{T,2})
   
for row = 1:size(m, 1)
        m
[row, findmax(m[row, :])[2]] = one(T)
        m
[row, findmin(m[row, :])[2]] = zero(T)
   
end
end



Two caveats. First, I'm guessing this won't be that fast, because it's traversing through rows instead of columns, but maybe you're not worried about speed.
Second, this doesn't handle ties (you didn't say how you wanted to handle them)

Mauricio Esteban Cuak

unread,
Oct 6, 2015, 10:46:02 PM10/6/15
to julia-users
Ups, it's nicer to use indmax and indmin, sorry

function change_min_max!{T}(m::Array{T,2})
    
for row = 1:size(m, 1)

        m
[row, indmax(m[row, :])] = one(T)
        m
[row, indmin(m[row, :])] = zero(T)
    
end
end

Rosangela Oliveira

unread,
Oct 8, 2015, 8:47:30 PM10/8/15
to julia-users
Hi Mauricio,

The output for that is "change_min_max! (generic function with 1 method) "

I didn't understand.
Reply all
Reply to author
Forward
0 new messages