Difficulty/confusion using subscripts in MATLAB - arrays

I am working with a code in MATLAB and i have to implement function y= 1-2x(t-1)
but when i try to code i get error.
How to get rid of this error?
clc
clear all
close all
t=-3:.1:3;
x=heaviside(t);
y=1-2*x(t-1)
plot(t,y)

There is a difference between evaluating a function and indexing an array, though they both use the same syntax in MATLAB.
Since x is an array, not a function, x(t-1) attempts to index into the array x, at locations t-1. However, t contains non-integer values and non-positive values. Indices in MATLAB must be between 1 and the number of elements in the array.
To shift an array by 1 to the right, you can use indexing as follows:
x([1,1:end-1])
Here, we repeat element #1, and drop the last element. There are other ways of accomplishing the same.
But, because one time unit does not correspond to one array element, since t is incremented by 0.1 every array element, this corresponds to a shift of 0.1 time units, not of 1 time unit.
To shift by one time unit, you’d have to modify the indexing above to shift the array by 10 elements. In the general case, it is posible that 1 time unit does not correspond to an integer number of array elements, for example if the increment had been 0.3 instead of 0.1. In this case, you need to interpolate:
interp1(t,x,t-1,'linear','extrap')
Here we are reading outside of the input array, and therefore need to take care of extrapolation. Hence that last argument to the function call. You can also choose to fill extrapolated values with zeros, for example.

Related

Find first non-consecutive array element using Google Sheets Filter

I have to make this work in a (always sorted) array that results from splitting a delimited string of sorted whole numbers. Concretely, if my cell contains "1,2,3,5" I need a formula to evaluate to 4. A beneficial side-effect would be to find an implementation that would give the last-number+1, if the original array had only consecutive numbers, i.e., applying the formula to "1,2,3,4,5" would evaluate to 6.
My approach has been to generate a new array that is a perfect sequence and compare it with my original array, to find the first element where the two arrays are not equal.
Creating a perfect sequence of the array like this:
=TRANSPOSE(SEQUENCE(COUNT(arr),1,MIN(arr),1))
So all that would be left to do is compare arr with the sequence above to find the first element that differed, something like:
=COUNTA(IFERROR(FILTER(arr;MATCH(arr; transpose(sequence(count(arr),1,min(arr),1))
;0))))
Sadly, what I have above is not correctly "short-circuiting" at the first non equal value of the arrays. Is COUNTIF the way to go?
If my previous step gets me the index of the element instead of the value, then what remains is to get the value at that index:
INDEX( arr, 1, counta(iferror(filter(arr;match(arr1; transpose(sequence(count(arr),1,min(arr),1))
;0)))) )
Is there a more straight-forward way get the first non-consecutive element? A way that does not involve actual ranges in the spreadsheet?
After some thought, I realized that set-subtracting (i.e. set difference) the original array from the generated sequence always gives me the first missing number.
So my final formula to handle this and the case where all numbers are in sequence is this:
IFERROR(INDEX(FILTER(TRANSPOSE(SEQUENCE(COUNT(SPLIT(G7," ")),1,LEFT(G7,4),1)), ISERROR(MATCH(TRANSPOSE(SEQUENCE(COUNT(split(G7," ")),1,LEFT(G7,4),1)),SPLIT(G7, " "),False))),1,1),RIGHT(G7,4)+1) )
I'm sure there is a better, more concise answer, but this does the job.

Find duplicates in an array in linear time

Problem: You are given an array of n+1 integers from range 1..n. At least one number has duplicate. All array values can be same. Print all duplicates in linear time and constant space. Array can't be modified.
The obvious solution would be to create a bit array with default value false, set 1 in bitarray[array[i]] for each element, then check if it's already 1. That requires additional space, so no good. My another thought: reorder the array by hash and check if a current element and the element array[hash % n] are equal. This is also no good since we can't modify the original array. Now I think that it looks like an impossible task. Is there even a solution to this?

Excel > Find first first/lowest non-zero value in unsorted array

Using a complex series of array combinations using standard Excel functions in a single formula (no VBA or UDF's involved), I've got the following result appearing mid-formula. I now need to add one final step to the formula to pull just one number out of this array:
{0,0,0,4,0,6,7...}
Using Excel formulas, how can I retrieve the first non-zero figure (in this example, 4) without any additional references to the array? Due to the complexity of the calculations it took to produce this array from the source data, I'd rather not do this twice in the same formula.
So I'm looking for functions or operators which can be applied to the array in order to yield the required result - ideally something elegant and simple. MATCH and LOOKUP fail because they require the array to be sorted. MIN fails because the lowest value is 0, not 4.
NB: The value of each non-zero figure also corresponds with its position in the array (the first would be 1, second would be 2, etc), so the first non-zero number will always be the smallest.
Try this
=1/AGGREGATE(14,6,1/{0,0,0,4,0,6,7},1)
It finds the maximum of the reciprocals ignoring the error values so the answer is 0.25. Then it takes the reciprocal of that.

Subtracting a number from all elements of an array in constant time

Can I subtract an integral value from all elements of an array in constant time?
For example:
Given array: 1 2 3 4
Expected array:0 1 2 3
I want this result in O(1) time complexity.Is this possible?
If yes,How can I achieve the same?
P.S.:The expression a[100]={0}; initializes all cells of array to zero without using the for loop.I am looking for similar expression
You can't change n elements in memory in less than O(n) time, but you can change what that memory represents. If you were to create a custom array class you can include an offset member. When an array element is read you add the offset on demand. When an element is added, you subtract the current offset before storing it in memory (so it is recalled as the correct value when added with the offset). With that layout simply modify the offset in O(1) time and effectively achieve what you are looking for.
No. You would need to go through every element and subtract one. Going through every element implies o(n) or linear time. Constant implies you would only need to go through one element.
a[100]={0} is syntactic sugar that appears to be constant but it's actually linear on the backend. See this answer

Continuously Numbers Insert to Array Labview

I having this number generator from value 4 to 4.999 each time, I wan to make it into 2 index array such as each time the number generates the first number will store in the first index array, while the second number will store in the second index array, then the third number will store back into the first index array and repeat continuously. So lastly I able to ramp the higher value in the array index to the lower value of array index and display in numbers of samples.
I have a vi here: http://i.stack.imgur.com/902X7.png which I don't what was the correct placement. Anyone can provide me any advices?
Please let me know if you don't understand my question..
Regardless of whether you're placing data in two separate arrays or just interleaving the data, you could perform a modulo operation to swap the index or the array. Wire the Loop iteration terminal of your loop to the Quotient & Remainder function, and divide by two. Every time the remainder is zero, your index is an even number, every time it's 1, it's an odd number. You can use those results directly as the index if you're using a single, two dimensional array, or you can use it to select between two different arrays by either using a case structure, or by converting the result into a boolean value and using it as the input to a Select function.

Resources