Wavelet transform using dwt3 in MATLAB - matlab

I have a project to transform an image using dwt.
I successfully done it using function dwt2, and now I try to use function dwt3 by changes some code from the function dwt2 (add more subband: 8 subbands). Unfortunately, an error comes out, which said "Too many output arguments".
My question is, what is the right way to write MATLAB code for dwt3? Is it not same as dwt2, just add more subbands?

Just by looking at the official documentation for dwt2 and dwt3, I see that dtw3 has only 1 output variable, whereas dtw2 has 4.
I assume you just replaced the string dtw2 in your code to dwt3, without paying attention to the amount of allowed output variables. So there you go, that's where the error "too many output variables" comes from...
If dwt3 only returns the transformed vector, cut the number of output variables to 1, and I'm sure the error will away:
Y = dwt3(X, 'db2');
Here I transformed X using dwt3 with the Daubechies 2-tap wavelet, and stored the result in Y.
P.S
You need to show more code if you want more productive, helpful answers...

Related

Using mean function in MATLAB yielding different results [duplicate]

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 3 years ago.
I have just recently started using MATLAB as it's pretty good for machine learning and the like.
Currently, I am working on some type of classification which is pretty long-winded and complicated if I tried to explain everything I am trying to accomplish therefore I will just state the exact code giving me problems.
So, I am given a 1010 x 1764 single type matrix by some function. say the matrix is called train_examples_2_2 as you can see on the right-hand side of the screenshot below.
As you can also see from the screenshot above (on the right-hand side), the calls to mean and std:
mean = mean(train_examples_2_2)
std = std(train_examples_2_2)
Yield the correct results.
However, when I run the same code several times sometimes I get an error on the line mean = mean(train_examples_2_2) stating:
Array indices must be positive integers or logical values.
The exact code I am concerned with is:
mean = mean(train_examples_2_2) % <----- error appears here
std = std(train_examples_2_2)
for i=1:size(train_examples_2_2,1)
train_examples_2_2(i,:) = train_examples_2_2(i,:) - mean;
train_examples_2_2(i,:) = train_examples_2_2(i,:) ./ std;
end
% end of standardisation process
where train_examples_2_2 is provided by some function that I did not create nor can modify.
According to the MATLAB documentation:
If A is a matrix, then mean(A) returns a row vector containing the
mean of each column.
which is what I get the first time I run the code upon opening Matlab but after that, it yields the aforementioned error.
I am using MATLAB R2018b.
I'm I making a simple mistake or could this possibly be a bug?
Thanks for taking the time to help out.
unlike let's say python you shouldn't/can't/mussn't re-define function names or default variables.
mean = mean(train_examples_2_2) % <----- error appears here
matlab doesn't distinguish between the callable mean() function and the variable ```mean``. especially confusing since indexing and calling sth is done by using round brackets.
So....?
call your variable sth. other than mean. mean_ will already do the trick.

How to get the zeros of the given equation using fzero in MATLAB?

I have the following function that I wish to solve using fzero:
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)}
Here, C, T_m, T_i, and L_f are all input by the user.
On trying to solve using fzero, MATLAB gives the following error.
Undefined function or variable 'X'.
(where X are the variables stated above)
This error is understandable. But is there a way around it? How do I solve this?
This is answered to the best of my understanding after reading your question as it's not really clear what you are exactly trying and what you want exactly.
Posting the exact lines of code helps a big deal in understanding(as clean as possible, remove clutter). If then the output that matlab gives is added it becomes a whole lot easier to make sure we answer your question properly and it allows us to try it out. Usually it's a good idea to give some example values for data that is to be entered by the user anyway.
First of to make it a function it either needs a handle.
Or if you have it saved it as a matlab file you generally do not want other inputs in your m file then the variable.
So,
function [out]=yourfun(in)
constants=your values; %you can set a input or inputdlg to get a value from the user
out= something something, your lambda thingy probably; %this is the equation/function you're solving for
end
Now since that is not all that convenient I suggest the following
%declare or get your constants here, above the function makes it easier
syms lambda
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)};
hf=matlabFunction(f); %this way matlab automatically converts it to a function handle, alternatively put #(lambda) in front
fzero(hf,x0)
Also this matlab page might help you as well ;)

Creating functions in Matlab

Hi, I am trying to write a function as per the question. I have tried to create four sub-matrices which are the reverse of each other and then multiply to give the products demanded by the question. My attempt:
function T = custom_blocksT(n,m)
T(1:end,end-1:1);
T(1:end,end:-1:1)*2;
T(1:end,end:-1:1)*3;
T(1:end,end:-1:1)*4;
What I'm unsure of is
(i) What do the the indivual sub-matrices(T(1:end,end-1:1);)need to be equal to? I was thinking of(1:3)?
(ii) I tried to create a generic sub-matrix which can take any size matrix input using end was this correct or can't you do that? I keep getting this error
Undefined function or variable 'T'.
Error in custom_blocksT (line 2)
T(1:end,end-1:1);
I have searched the Matlab documentation and stacked overflow, but the problem is I'm not quite sure what I'm supposed to be looking for in terms of solving this question.
If someone could help me I would be very thankfull.
There are many problems with your function:
function T = custom_blocksT(n,m)
T(1:end,end-1:1);
T(1:end,end:-1:1)*2;
T(1:end,end:-1:1)*3;
T(1:end,end:-1:1)*4;
end
This is an extremely basic question, I highly recommend you find and work through some very basic MATLAB tutorials before continuing, even before reading this answer to be honest.
That said here is what you should have done and a bit of what you did wrong:
First, you are getting the error that T dos not exist because it doesn't. The only variables that exist in your function are those that you create in the function or those that are passed in as parameters. You should have passed in T as a parameter, but instead you passed in n and m which you don't use.
In the question, they call the function using the example:
custom_blocks([1:3;3:-1:1])
So you can see that they are only passing in one variable, your function takes two and that's already a problem. The one variable is the matrix, not it's dimensions. And the matrix they are passing in is [1:3;3:-1:1] which if you type in the command line you will see gives you
[1 2 3
3 2 1]
So for your first line to take in one argument which is that matrix it should rather read
function TOut = custom_blocks(TIn)
Now what they are asking you to do is create a matrix, TOut, which is just different multiples of TIn concatenated.
What you've done with say TIn(1:end,end-1:1)*2; is just ask MATLAB to multiple TIn by 2 (that's the only correct bit) but then do nothing with it. Furthermore, indexing the rows by 1:end will do what you want (i.e. request all the rows) but in MATLAB you can actually just use : for that. Indexing the columns by end-1:1 will also call all the columns, but in reverse order. So in effect you are flipping your matrix left-to-right which I'm sure is not what you wanted. So you could have just written TIn(:,:) but since that's just requesting the entire matrix unchanged you could actually just write TIn.
So now to multiply and concatenate (i.e. stick together) you do this
TOut = [TIn, TIn*2; TIn*3, TIn*4]
The [] is like a concatenate operation where , is for horizontal and ; is for vertical concatenation.
Putting it all together:
function TOut = custom_blocks(TIn)
TOut = [TIn, TIn*2; TIn*3, TIn*4];
end

Making scatter plots from structs in MATLAB

I'm trying to write a bit of code of to make MATLAB scatter plots from variables in a structure. I want to give the code the name of the structure (there will be many of these structures) and then get it to make a scatter plot of two variables. When I try the code below I get an error message saying, "??? Error: File: make_graphs.m Line: 6 Column: 9
The input character is not valid in MATLAB statements or expressions."
str2stuct= input('Please enter the string for the struct e.g. TMB_RUN_1_data:');
test1=strcat(str2stuct,'.NDROP_max');
test2=strcat(str2stuct,'.input_kappa');
scatter($(test2), $(test1))
I thought that the error message probably meant that I was using the dollar sign in a way which MATLAB doesn't approve of (I've yet to find much use for $ in MATLAB).
I tried it like this:
str2stuct= input('Please enter the string for the struct e.g. TMB_RUN_1_data:');
test1=strcat(str2stuct,'.NDROP_max');
test2=strcat(str2stuct,'.input_kappa');
scatter((test2),(test1))
And got this error:
"??? Error using ==> scatter at 51
Must supply X and Y data as first arguments.
Error in ==> make_graphs at 6
scatter((test2),(test1)) "
I tried it with changing the last line as shown below but got the same error as with the brackets:
scatter(test2,test1)
If I use the literal name as below it works fine.
scatter(TMB_RUN_1_data.NDROP_max,TMB_RUN_1_data.input_kappa)
I've tried a bunch of other things but I am not getting it. I've tried the mathworks pages on scatter but there are no examples that are close to what I am doing. I am really really stuck.
EDIT: I have found a solution but I am aware that this is not considered best practice. If you can simply explain how to do this better that would be good. Answers should be aimed at a moron in a hurry, not an experienced programmer.
Making this the last line works:
scatter(eval(test2),eval(test1))
I am aware that 'eval' is frowned upon and so this probably isn't a good long term answer, works for now. This seems to be the way to get MATLAB to actually read the contents of the strings test1 and test2 into the lines in question.

Effcient way to do FFT shift in matlab (without using fftshift function)

http://www.mathworks.com/help/techdoc/ref/fftshift.html
If you check that link - thats what I want to do in the first picture - swap quadrants of a matrix.
However, I cant seem to think of a good way to do this without having several loops to pull out the relevant sub-matrices.
I need it to work with MxN matrices, where M and N can be any combination of even and odd.
Thanks
The following should work
sz = ceil(size(A)/2)
A = A([sz(1)+1:end, 1:sz(1)], [sz(2)+1:end, 1:sz(2)])
That only works for 2d matrices, but can be easily generalized to the Nd case.
If you enter type fftshift.m at MATLAB's command line, you'll see the source code for MATLAB's implementation of the function (use edit fftshift.m if you want to view it in the editor with syntax highlighting). I'm not posting the code here, as it is copyrighted. However, you can try it on your machine and re-implement the same in C. Its up to you to figure out the license terms etc, if you're into any of that.