I am very new to matlab and I am attempting to loop a list of values through a function. So that I get a new output for every value.
This is what I have tried so far:
listvalues = [1, 4, 6, -2];
for i = 1:length(listvalues)
output_function = output_function * 10^(listavalues(1)/20)
end
I am not sure where to connect the loop in the function.
Could anyone help with this?
Related
I am trying to create a user-defined function that can assign values (in this case ones) to an array of zeros which is in another script. Is there any way to do that?
I have tried using the code below, I am not sure what I did wrong because it wouldn't update the values of my array at all. I am new to programming!
% Main file
n = zeros(3);
assignval(0, 2, n);
% User-defined function
function assignval(s, e, n)
for i = s:1:e
n((i+1),1) = 1;
end
I expect that the whole first row values to be 1, but instead after running the program it is still zero. Your help is really appreciated!
Try the below code
% Main file
n = zeros(3)
n2 = assignval(0, 2, n)
% User-defined function
function [n] = assignval(s, e, n)
for i = s:1:e
n((i+1),1) = 1;
end
end
I work with fcm in MATLAB. I need to turn off logging in command windows. What is the best way to accomplish this?
For example, when I run the command I get the following printed to the MATLAB command window
>> fcm(dok, 7)
Iteration count = 1, obj. fcn = 8.970479
Iteration count = 2, obj. fcn = 7.197402
Iteration count = 3, obj. fcn = 6.325579
Iteration count = 4, obj. fcn = 4.586142
You can set the fourth element of options array input to 0 to indicate that you don't want to display the results from every iteration.
[centers, U, objfun] = fcm(dok, 7, [2, 100, 1e-5, 0])
Alternately, you can use evalc to suppress all command line output from a function.
[~, centers , U, objfun] = evalc('fcm(dok, 7)');
I am new in GUI Matlab. I want to have a table of values in GUI, which get results from m.file. The table is of 9x10. (9 parameters, each will get 10 results). In m.file, I have a loop of getting the results of these 9 parameters and this loop iterates 10 time. I want to save the result 9 parameter per iteration of loop in a row of this table. How I can proceed? Thanks in advance.
You can save them into a 9x10 matrix and loop writing parameters into it. Like this:
res = zeros(9, 10);
for i=1:9
for j=1:10
res(i, j) = ...
end
end
I've got a problem with my function. It's supposed to take the signal and time data and output two vectors of the times of the maximums and the maxima signals. What I've done is try to use the findpeaks function to create a pks and locs array, then initialise a blank array the same size as the locs array and then use a for statement to go through and reassign the zeros with the time data of the maximas.
function [ max_times, max_signal ] = local_max(time_data, signal_data)
%Finds the local maximum of data
[pks, locs] = findpeaks(signal_data);
max_times = zeros(size(locs));
for n = 1:size(locs);
max_times(n) = max_signal(locs(n));
end
clear 'locs'
end
This is the error I get:
Error: File: local_max.m Line: 7 Column: 10
The expression to the left of the equals sign is not a valid target for an assignment.
I don't quite understand what it means/ how I can solve it. Anyone able to help?
The reason why you get an error is because max_signal is not defined when you use it. You probably want:
max_times(n) = time_data(locs(n));
Also, looping is unnecessary. You can simply do:
[pks, locs] = findpeaks(signal_data);
max_times = time_data(locs);
The reason is that size(locs) will return [1, n], not a number. So you need to change
for n = 1:size(locs);
to
for n = 1:numel(locs);
I'm processing data in parallel using parfor in this way:
iteration = 10;
result = zeros(1, iteration);
matlabpool open local 2
parfor i = 1:iteration
data = generate_data();
result(i) = process_data(data);
end
end
matlabpool close
It works fine, but I have one problem. My function generate_data generates unique data (i.e. 0, 1, 2, 3, 4 ...) but in practice sometimes I give same value two times (and I give 0, 1, 1, 2, 3, 4, 4, 5, ...). In simple my function looks like this:
function data = generate_data()
persistent counter generated_data;
if(isempty(counter))
counter = 1;
generated_data = [0 1 2 3 4 5 6 7 8 9];
end
data = generated_data(counter);
counter = counter + 1;
How can I fix this ?
If I've understood correctly, you want to ensure that your generate_data doesn't return the same value to two iterations of your PARFOR loop. Unfortunately, you cannot do this directly in the PARFOR loop, since no communication is allowed. Your options are basically: either call generate_data on the MATLAB client; or run two PARFOR loops perhaps like this:
parfor ii = 1:iteration
generated(ii) = generate_data();
end
% omit duplicated values - perhaps you might wish to generate
% some more here too...
generated = unique(generated);
parfor ii=1:numel(generated)
result(ii) = process_data(generated(ii));
end
I tried this:
function data = generate_data(id)
persistent generated_data;
if(isempty(generated_data))
generated_data = [0 1 2 3 4 5 6 7 8 9];
end
data = generated_data(mod(id - 1, length(generated_data)) + 1);
In each call I can generate data set on demand using id. I have to remember that one data set can be reached only for one id. It works, but not solve my problem. I would eliminate parametr id and use internal counter like in first post.