help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Matrix assignment question


From: Jordi Gutiérrez Hermoso
Subject: Re: Matrix assignment question
Date: Wed, 20 Feb 2013 16:25:18 -0500

On 20 February 2013 16:00, Nir Krakauer <address@hidden> wrote:
> If I have
>
> A = ones(2);
> X = [2 NaN; NaN 3];
>
> and I want to update A to reflect the values in X (excluding missing
> values), I can do
>
> i = isfinite(X);
> A(i) = X(i);
>
> However, if X only corresponds to a part of the matrix I want to
> update, this become more complicated. If
>
> B = ones(3);
>
> the following expression is illegal:
>
> B(1:2, 1:2)(i) = X(i);

How odd, someone just asked this very question in #octave a few hours
ago too. You can work around it like so:

    B( sub2ind (size (B), nthargout(1:2, @find, i){:})) = X(i)

which admittedly is quite ugly.

The issue here is basically that Matlab's syntax sucks and we can't
fix it here. There is no way to fix it because when you write, e.g.

    sin(1:3) = 5

that's really

    sin (1:3) = 5

i.e. you're creating a new variable sin and assigning its first three
entries equal to 5 (thus hiding the sine function). So the syntax
cannot disambiguate something like

    sin(1:3)(2) = 5

i.e. can't tell what kind of temporary you're indexing again (if it's
an array or the output of a function) and then trying to assign to it.

The cryptic error message about "close the index chain", which really,
needs to be fixed, is about how when you chain multiple
x(...)(...)(....) subscripting, you can't put anything after the last
(...) except doing a dot-indexing:

    [struct("foo",1), struct("foo",2), struct("foo",2)](1:3)(1:2).foo

In particular, after the last (), you can't put an assignment.

HTH,
- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]