I want to substract two arrays in Matlab but they are of different sizes. When I try to substract them, it says the following:
??? Error using ==> minus
Matrix dimensions must agree
Does anyone has a clue?
Thanks in advance,
#Jens Björnhager is correct: the transpose() function will do what you want, which is to flip one of your input vectors from a row-vector to a column-vector.
Alternately, Use the ' operator. A' is shorthand for transpose(A).
Try the transpose() function to make the sizes match.
Related
Hello I wanted to reshape this code to make it easier.
sins=1/3*(sind(50)+sind(300)+sind(340));
coses=1/3*(cosd(50)+cosd(300)+cosd(340));
result=atand(sins/coses);
it will be more input like 50,300,340... so I wanted to like this
a=[50 300 340];
sins=1/3*(sind(a));
coses=1/3*(cosd(b));
result=atand(sins/coses);
but it doesnt work.
How can I make it ?
Thanks in advance.
In your first example, you sum all three values returned by sind, in your second code you don't sum them. Inputting a matrix, sind (like most mathematical functions in MATLAB) returns a matrix of the same size, applying the function to each element. Use sum to get the sum of a vector.
sins=1/3*(sum(sind(a)));
I have image, and I want to do up sampling. First of all I need to plug zeros between pixels such that [1,2,3] transforms to [1,0,2,0,3]. Can anyone tell me how to do it without using paddarray and without using for loops?
Thank you in advance!
Something like this?:
B=zeros(size(img)*2);
B(1:2:end,1:2:end)=img;
However there are ways of up-sampling in matlab without having the need of doing it by hand, for example interp2
You could also make use of MATLAB's way of dynamically allocating variables if you don't specify a number for an index into the array. By omitting indexing into certain locations in your array, MATLAB will fill in these values with zeroes by default. As such:
B(1:2:5) = 1:3
B =
1 0 2 0 3
V = [1,2,3];
padded(numel(V)*2) = 0;
padded(1:2:end) = V
And then just deal with the trailing zero if numel(V) was odd
There is a function upsample that does exactly this from Octave-Forge -- see docs on upsample.
Or you can look at the source of upsample to see what implements it. Are you opposed to using a package or a function?
I run
pdesurf(mesh.p, mesh.t, u)
I got
Error using pdesurf (line 25)
Illegal solution format.
PDESURF expects input of the form pdesurf(p,t,u). u must either be a column vector and the same length as p, or a row vector and the same length as t. I don't know how big your mesh.p and mesh.t variables are, so I can't say for sure, but it could be because you need to transpose your vector. It is possible that the error might be corrected by changing your code to
pdesurf(mesh.p, mesh.t, ufun(0:0.01:1,0:0.01:1)') % Note the transpose
If this doesn't work, then you need to make sure that either
size(t,2)==size(u,2)
or
size(p,2)==size(u,1)
Transpose u
pdesurf(mesh.p, mesh.t, u')
In Matlab I'm trying to find points in a 3d matrix whose coordinates are smaller than some function.
If these coordinates are equal to some functions than I can write:
A(some_function1,some_function2,some_function3)=2;
But what if I want to do something like:
A(<some_function1,<some_function2,<some_function3)=2;
This isn't working - so what is the other way of finding such points without using "for" loop? Unfortunately with "for" loop my code takes a lot of time to compute. Thank you for your help!
How about something along the lines of
A( ceil(min(some_function1,size(A,1))),...
ceil(min(some_function2,size(A,2))),...
ceil(min(some_function3,size(A,3))) );
This will cap the indicies to the end of each array dimension
You can just use regular indexing to achieve this:
A(1:floor(some_function1),1:floor(some_function2),1:floor(some_function3)) = 2;
assuming you check / ensure that floor(some_function*) is smaller than the dimensions of A
Try:
A(1:size(A,1)<some_function1, 1:size(A,2)<some_function2, 1:size(A,3)<some_function3) = 2
I hope I got your question correctly.
I need to add a new matrix to a previously existant matrix, but on his dimension coordinate.
I know this is hard to understand, so let's see it on a example:
I've a matrix like this:
480x640x3
And I want to add the following one:
480x640x6
The result has be this: (6+3 = 9)
480x640x9
As you can see it adds but on the 3rd dimension.
For concatenating along higher dimensions, use the function CAT:
newMatrix = cat(3,matrix1,matrix2);
I would say that gnovice's answer is probably the best way to go, but you could do it this way too:
matrix1(:,:,4:9) = matrix2;