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)');
Related
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?
How do i print a vector in matlab with also something before the values. For example i have a vector with the following values A' = [1 2 3]. I want to print it out in such a way that the output will be
w0 = 1
w1 = 2
w2 = 3
How do i do this?
Can i do this using a loop?
I am using the following code and i am not getting the right output
for qux = 1:3
fprintf('w%i=%.4lf\n',qux-1,answ);
end
Output:
w0=w1=w2
Your format string is not formed properly. Specifically '%.4lf' should be '%.4f'. Additionally, the third input to fprintf should be A(qux) to access the value in A.
for qux = 1:3
fprintf('w%i=%.4f\n', qux-1, A(qux));
end
I would, however, recommend using '%g' to use a format that optimizes the display of each number. Additionally, you could remove the for loop and do something like
A = [1, 2, 3];
fprintf('w%i = %g\n', [0:numel(A)-1; A])
If I have understand the question correctly, I think that you could do that with the following code:
for i = 1:3
disp(['w' num2str(A(i)-1) '=' num2str(A(i))]);
end
Using disp and num2str you could get the following output:
w0=1
w1=2
w2=3
I'm using the code
k = 0;
while k<3
k = k+1;
a = 5^k;
disp(a);
end
however, when the result outputs it only gives me the answer of one iteration. I'm wondering what the difference is to the computer when you use this code instead:
clear, clc
k = 0;
while k<3
k = k+1;
a(k) = 5^k;
end
disp(a)
Why does the first code sample output only 125, while the second one outputs 5, 25, and 125?
In the first code, variable a is scalar.
So, Matlab erases and re-writes value into variable a in every iteration.
But, in case of second code, as you defined array index k at variable a, Matlab understands your variable a(k) as array variable. And, in every iteration, Matlab writes the assigned value 5^k on corresponding array point.
I seem to have a problem with my knapsack algoritm on MATLAB. I have several files to add to the storage and they have certain values. I would like to maximize the total value under a certain capacity restriction.
The values are 10, 40, 30, 50.
The sizes are 6, 4, 5, 3.
The capacity is 10.
When I run the code, I get the optimal total value but I cannot specify which files have been added to the storage. Is there a way to do this? I tried adding the file numbers into an array but it wouldn't work. The array is emptied every time the function is run.
The code I have is as follows:
function maxim=addfiles(v,w,cap,n)
maxim=0;
stored=[];
if (cap==0)||(n==0)
return
else
if w(n)>cap
maxim=addfiles(v,w,cap,n-1);
else
if addfiles(v,w,cap,n-1)<v(n)+addfiles(v,w,cap-w(n),n-1)
maxim=v(n)+addfiles(v,w,cap-w(n),n-1);
stored(n)=n;
else maxim=addfiles(v,w,cap,n-1);
end
end
end
What I want from the algorithm is to return something like:
Optimal value: 90
Files added: 2,4
Thank you in advance.
I rewrote your code so it will return the maximum value and the selection; not sure if it's more MATLABish than you're used to, but it does the same thing, and is better commented :-D
%// Knapsack problem
%// v = values (non-negative, vector)
%// w = weights (positive, vector)
%// cap = available capacity (non-negative, scalar)
%// n = the remaining number of objects (non-negative, scalar)
%//
%// maxim = max accumulated value (non-negative, scalar)
%// stored = index of stored objects (integer, vector)
function [maxim, stored] = addfiles(v,w,cap,n)
%// End of recursion
if (cap==0)||(n==0)
maxim = 0;
stored = [];
return;
end;
%// Above capacity
if w(n) > cap
[maxim, stored] = addfiles(v,w,cap,n-1);
return;
end;
%// Alternatives 1/2
[maxim1, stored1] = addfiles(v,w,cap,n-1);
[maxim2, stored2] = addfiles(v,w,cap-w(n),n-1);
if maxim1 < v(n) + maxim2
%// Adding is good
maxim = v(n) + maxim2;
stored = [stored2, n];
else
%// Adding is bad
maxim = maxim1;
stored = stored1;
end;
end
Save in addfiles.m, then call with:
[maxim,stored] = addfiles([10, 40, 30, 50], [6, 4, 5, 3], 10, 4)
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.