[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: trying to optimize my octave program
From: |
Judd Storrs |
Subject: |
Re: trying to optimize my octave program |
Date: |
Fri, 19 Mar 2010 12:35:07 -0400 |
2010/3/19 Tim Rueth <address@hidden>:
> If ndgrid makes a 4-D matrix which has, say, 256 elements
> (4 in each dim), then I'll be creating 256 copies of several vectors inside
> analyze(), each which has a length of 4288. I started implementing this but
> hit two snags:
>
> 1) How can I initialize a multidimensional array with, say "foo" in each
> element, without a for loop? Exactly like zeros(...) but filling each
> element with "foo" instead of 0.
repmat() is the fastest way to do this e.g.
a = repmat(123.456,[7 8 9]) ; # Numeric matrix
a = repmat("foo", [7 8 9]) ; # Character matrix
a = repmat({"foo"},[7 8 9]) ; # Cell matrix
> 2) How do I do element-wise comparisons and conditional string assignments
> on matrices without stepping through them? For example, let's say I have
> two 4-D matrices, a_mat and b_mat with numeric values. I'd like to create a
> new 4-D string matrix, c_mat, where
>
> for each i in a_mat
> if a_mat(i) < b_mat(i)
> c_mat(i) = "foo";
> cntr++;
> endif
> endfor
The trick is to use logical arrays for indexing in the assignment.
Assuming c_mat is a cell array:
a_mat = rand([4 5 6 7]) ;
b_mat = rand([4 5 6 7]) ;
c_mat = repmat({""},[4 5 6 7]) ;
c_mat(a_mat < b_mat) = "foo" ;
Another option is:
idx = find(a_mat < b_mat) ;
c_mat(idx) = "foo" ;
cntr = length(idx) ;
--judd
- Re: trying to optimize my octave program, (continued)
- Re: trying to optimize my octave program, Jordi Gutiérrez Hermoso, 2010/03/17
- Re: trying to optimize my octave program, Jaroslav Hajek, 2010/03/18
- RE: trying to optimize my octave program, Tim Rueth, 2010/03/18
- Re: trying to optimize my octave program, Judd Storrs, 2010/03/18
- Re: trying to optimize my octave program, Judd Storrs, 2010/03/18
- RE: trying to optimize my octave program, Tim Rueth, 2010/03/19
- Re: trying to optimize my octave program,
Judd Storrs <=
- RE: trying to optimize my octave program, Tim Rueth, 2010/03/21
- Re: trying to optimize my octave program, Jaroslav Hajek, 2010/03/19
- Re: trying to optimize my octave program, Jaroslav Hajek, 2010/03/19
- RE: trying to optimize my octave program, Tim Rueth, 2010/03/19