0

Still trying to grasp around the basics of Matlab.

I have a function f, that takes as arguments a matrix A, and two numbers (max and delta).

I also have a function g that takes a matrix A and one number threshold and returns a matrix B, if the value of an element in A is bigger than or equal to threshold, the corresponding value in B should be 1, if it is smaller than or equal to -threshold, it should be -1, otherwise 0. The original matrix should not be changed in neither function f nor g.

I want function f to try different values for threshold in the call for g, and I want the results from each call to line up horizontally in a new matrix.

I'm not sure how to do this function f, I'm guessing the easiest way would be to create an array:

 t1= [-threshold:delta:threshold]; 

but how do I call g for each value of the elements in the array and line them up in a new array?

function B = f(A, threshold, delta)
    t1= [-threshold:delta:threshold];
    %What to write here?
end

function B = g(A, threshold)
    B=(A>=threshold)-(A<=-threshold);
end

If

A=[[-3:-1]' [1:3]']

Then

f(A, 2, 1)

should return the same matrix as the command [[-1 -1 0]' [0 1 1]' [-1 -1 0]' [0 1 1]']

1 Answer 1

1

What you want is a loop statement, a for loop would do well here.

As for lining the arrays side by side, simply create an array of the right size:

h = size(A,1);
w = size(A,2);
result = zeros( h , w*floor(2*threshold/delta) );

Then in each iteration index the correct section:

result(:,((i*w):((i+1)*w)+1) = g(A, -threshold+delta*i);

Although, I must say, it's somewhat of an odd way to store data, would be better to use a cell array, or a 3D matrix, something like this:

result = zeros( h , w , floor(2*threshold/delta) );

and

result(:,:,i+1) = g(A, -threshold+delta*i);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Why is it an odd way to store data? I have a number of signals, stored in A, I want to try to discretize them at different tresholds and see if the correllation with a goal signal is higher. For me it made sense to line them up next to each other and then doing a correlation against a vector. Is it still wierd?
There's no problem with it if it serves your purpose, it just makes the data hard to access, as you can see in the second code block in my answer the indexing becomes very ugly, plus you need to know the width of a single data set, where in the forth block the indexing is much simpler, it is, result(x coordinate, y coordinate, threshold index). How are you doing the correlation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.