From: omegayen on
Hi, So I am not quite sure the best way to code this is... as a simple example let me say the following

essentially I have a large number of cases that all have the same variables, lets call it a, b, and c, although can be different values for different cases

I want to calculate using a value for each case using a formula based on the variables

is there any way to do this without having to define a ton of variables in MATLAB?

essentially it would look like this, but i dont know how to code this properly. thanks for your help

%case 1

a=5;
b=2;
c=3;

%case 2

a=4;
b=6;
c=7;

%case 3

a=1;
b=2;
c=7;

%calculate value for each case

value= a + b/c;

From: Matt Fig on
Use arrays!

% Data
a = [5 4 1];
b = [2 6 2];
c = [3 7 7];

% Create the value array.
value = a + b./c

Now we see: value(n) = a(n) + b(n)/c(n)
From: omegayen on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <hsl6jt$fm1$1(a)fred.mathworks.com>...
> Use arrays!
>
> % Data
> a = [5 4 1];
> b = [2 6 2];
> c = [3 7 7];
>
> % Create the value array.
> value = a + b./c
>
> Now we see: value(n) = a(n) + b(n)/c(n)

thanks matt, i see what your saying...

however I was hoping for something that will let me easily know which case is assigned to value.... this is because the different cases will not be 1, 2,3... ect, I'd prefer to assign them a string instead of a number

so ideally doing something like

value.case1

value.case2

value.case3

would pull the correct value instead of having to map numbers to strings....
From: Walter Roberson on
omegayen wrote:
> "Matt Fig" <spamanon(a)yahoo.com> wrote in message
> <hsl6jt$fm1$1(a)fred.mathworks.com>...
>> Use arrays!
>>
>> % Data
>> a = [5 4 1]; b = [2 6 2]; c = [3 7 7];
>>
>> % Create the value array.
>> value = a + b./c
>>
>> Now we see: value(n) = a(n) + b(n)/c(n)
>
> thanks matt, i see what your saying...
>
> however I was hoping for something that will let me easily know which
> case is assigned to value.... this is because the different cases will
> not be 1, 2,3... ect, I'd prefer to assign them a string instead of a
> number
>
> so ideally doing something like
>
> value.case1
>
> value.case2
>
> value.case3
>
> would pull the correct value instead of having to map numbers to
> strings....

value = struct('case1', 5, 'case2', 4, 'case3', 1);
a = [value.case1 value.case2 value.case3];

and the moral equivalent of switch becomes value.(currentcase) where
currentcasse is 'case1', 'case2', etc.