Why doesn't this function declaration work?
1 view (last 30 days)
Show older comments
Andrew Morris
on 27 Oct 2021
Commented: Walter Roberson
on 30 Oct 2021
I'm sorry that this is an extremely basic question that people might get irritated by, but I am new and I am trying to do some scripting. I want to declare a function that makes an array for concentration values of a serial dilution. As far as I can tell, I am following the syntax matlab gives for function EXACTLY, but it still gives an error when i call the function with what should be valid parameters.
For example, this should return a 24 by 1 array that is [ 25, 15, 9, ... ]
input:
a = serialdilution(25, 1.6666, 24)
output:
Error: File: serialdilution.m Line: 1 Column: 28
with this function definition in the \MATLAB path named serialdilution.m
function concentrations = serialdilution(maxconc, factor, n)
%returns an n by 1 array whose last element is maxconc
%and each previous value is equal to the next element divided
%by factor
for i=1:n
concentrations(n-i-1) = maxconc*(1/factor^(i-1);
end
concentrations = concentrations.';
end
0 Comments
Accepted Answer
Stephen23
on 27 Oct 2021
Edited: Stephen23
on 27 Oct 2021
You are missing a closing parenthesis here:
concentrations(n-i-1) = maxconc*(1/factor^(i-1);
% ^ missing closing parenthesis
Once you fix that syntax error you will get another error message due to your indexing (n-1-i), because as soon as i = n-1 then your index will be zero.
As far as I can tell, your task does not require a loop anyway:
x = 25;
f = 5/3;
n = 24;
x./f.^(0:n-1)
4 Comments
Walter Roberson
on 30 Oct 2021
Given a starting address for an array, base, and an index that is 0 based, you can find the in-memory address of the element by calculating base + (index * size_of_element) .
On some architectures, that might even be a single addressing mode, potentially. For example, one of the x64 addressing modes is Base + (Index * Scale) where Scale has a value of 1, 2, 4, or 8 .
Now, if you have a starting address for an array, base, and an index that is 1 based, you can find the in-memory address by calcuating |base + ((index - 1) * size_of_element) . Which certainly looks more complicated.
But... the difference compared to the other form is subtracting off size_of_element -- base + (index * size_of_element) - size_of_element and x64 also has an addressing mode Base + (Index * Scale) + Displacement and if you set Displacement to have a value equal to size_of_element then you have not used any more instructions... but you might have used a longer instruction word. Does the longer instruction word take enough longer to decode in microcode to matter? That isn't clear.
And if it does take enough longer to decode with the Displacement to be meaningful... then reduce the base by that value and suddenly it takes the same length of time. You can even pass around the pre-decremented base address, adjusting it if you ever typecast the pointer to refer to a different data type, but easier if you just pass around the original base and decrement it when you load it into the register to start your indexing loop.
Thus, even if you have hardware assistance on indexing, there is probably little hardware penalty for doing 1-based indexing in a loop. Maybe a one-time adjustment when you load the base address.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!