help-octave
[Top][All Lists]

## Re: Location Programming

 From: Nicholas Jankowski Subject: Re: Location Programming Date: Wed, 15 Feb 2017 10:27:07 -0500

```On Wed, Feb 15, 2017 at 5:39 AM, Nicholas Ablett
>
> What I can’t do currently or understand is:
>
> Why the formula for B does not need any reference to matrix A?
>
> How to save an alpha-numeric code into a matrix location (There will be
> multiple codes per matrix location as well)
>
> How to get the program to “spit out” the matrix location after entering the
> alpha-numeric code.
>
> How to get the matrix location values to automatically transfer into the
> equation for B.
>
>

It's good you're breaking down the problem into manageable pieces. And
thanks for telling us the overall intent. (It avoids wasting time
solving problems that maybe weren't needing in the first place. See
xyproblem.info for an educational explanation.)

You've stored no useful information in A.  it's just an array of
zeros. it has the size from m and n, but m and n already contain that
information, so it's not clear what you want to use A for.  your
current B doesn't reference A because you've just put arbitrary values
in the formula.

But, assuming you put your location codes in something called 'A'.
then you find which locations in A to use for calculation, then your
(location1 - location2) formula would need to reference that
information. You'll need a variable to hold that location info, and
then your '[1,1]-[3,3]' line would be replaced by something like
'location1-location2'

- alpha numeric code into a matrix:
assuming you need to store your code as a string and later do a
string comparison, it's probably easiest to create a cell array.
matrices can store strings, but only one character per location. a =
'hello' is actually a(1,1) = 'h', a(1,2) = 'e', etc.  All strings need
to be the same length, and you'd have to do some 3-dimensional
trickery to store your strings in a matrix.  Better is to use a cell.
A cell array can hold arbitrary things in each location. Cells are
(generally) referenced with curly braces. eg.,

>> b{1,1} = 4;
>> b{1,2} = 'hello';
>> b{2,1} = 'world';
>> b{2,1} = false;

>> b
b =
{
[1,1] =  4
[2,1] = world
[1,2] = hello
[2,2] = 0
}

So, you should be able to create an array with each cell location
cells here:
https://www.gnu.org/software/octave/doc/v4.2.0/Basic-Usage-of-Cell-Arrays.html
https://www.gnu.org/software/octave/doc/v4.2.0/Creating-Cell-Arrays.html

Not really sure what you mean by 'multiple codes per matrix location'

- spitting out a location after entering a code:
first what do you mean by 'entering a code'? are you prompting the
user to type in two codes/locations?  do you know how to do that?
Assuming you do, and you store the users input as a string, you will
then need to do a string comparison with those in the cell array you
created above. (note the command below will fail if every cell
location isn't a string)

>> b{1,1} = 'world';
>> b{1,2} = 'hello';
>> b{2,1} = 'hello';
>> b{2,2} = 'world';

>> strcmp(b,'hello')
ans =
0  1
1  0

OR

>> find(strcmp(b,'hello'))
ans =

2
3

you can then use the location(s) in whatever way you find useful.

```