Error using - Matrix dimensions must agree - matlab

I got the following error while working with a MATLAB program:
Error using - Matrix dimensions must agree
I noticed that the sizes of the matrices I'm trying to subtract from each other were:
firstMatrix --> 425x356
secondMatrix --> 426x356
How can I make them of equal size and go ahead and do my subtraction process?
I tried reshape, but the number of elements here seem to have to be equal.
Thanks.

I think both answers are missing the key point. Blithely subtracting two arrays of different size forgets that those arrays are NOT just numbers. The numbers must mean something. Else, they are just meaningless.
As well, simply deleting a row from the beginning or end may well be wrong, or padding with zeros. Only you know what the numbers mean, and why those arrays are not the same size. So only you can decide what is the proper action.
It might be right to pad, delete, interpolate, do any of these things. Or you might realize there is a bug in your code that created these arrays.

Your matrices have a different number of elements, so there's no point using reshape here (since it maintains the total number of elements). You'll have to discard one of the lines in the larger matrix before doing the subtraction:
For instance, you can discard the last line:
firstMatrix - secondMatrix(1:end - 1, :)
or discard the first line:
firstMatrix - secondMatrix(2:end, :)
Alternatively, you can pad the smaller matrix with default values (e.g NaN or zeroes), as suggested in another answer.

You're missing a row in firstMatrix
So can try:
firstMatrix=[firstMatrix;zeros(1,356)];
This will add a row of zeros at end of firstMatrix making it of 426x356

Related

How can I reduce the number of rows of a matrix by half in matlab?

What is the best way to reduce the number of rows of a matrix by half in matlab?
What is the following command doing?
mymatrix = mymatrix(1:2:end,:);
Is there any better way available?
Short answer this is taking every second row of the matrix mymatrix starting with the first one (all odd-rows) and yes that is probably the easiest way. Added clarification based on comment from #Sardar_Usama
Longer version
end is matlab internal command that refers to the end of the array in the given dimension. roughly equivalent to size(var,dim).
so actually what mymatrix(1:2:end,:) can be re-written to mymatrix(1:2:size(mymatrix,1),:). Now if you actually look at 1:2:size(mymatrix,1) these are the rows that you are selecting.1, 3, 5, etc. You can actually specify whichever rows you want there, here are some examples.
1:floor(end/2); % first 'half'
floor(end/2)+1:end; % second 'half'
1:3:end; % every third element
1:2:floor(end/2); % every second element in the first 'half'
Added floor() to avoid problems for odd number lengths. In that case 'half' is not exactly half but rather roughly half. Alternatively ceil() depending on how you would like to define half for odd lengths.

Selecting columns/rows of a matrix in Julia

this is a very basic problem but I didn't find any hints on it. Let's say I have a 2x4 matrix and I want to reduce the dimension of the matrix to only these columns that are in the sum larger than 1:
A=rand(2,4)
ind = sum(A,1).>1
That gives me an indicator of the columns I want to retain. Naively one would assume that I can do that:
A[:,ind]
which doesn't work as ind is a BitArray and only for Bool Arrays this is allowed, i.e., the following works
A[:,[true,true,false,true]]
in return, the following does work:
A[A.>0.5]
But it returns a vector of filtered elements.
What is the logic behind this and how do I solve my problem?
As noted in the comments, this is fixed by using a version of Julia which is >=v0.4.

Extract data using Matlab

How could I correlate the first (also second and third, etc) line of each function together? I meant I want to pick up the value of the first line of function 1, first line of function 2 and so on... Specially number of these lines (between two #...#) are not always equal.
Let's say the values between (# -#) is one matrix. I see that the problem is I need to break them down into different matrix and then equalize all these matrix to the same size by adding NaN values and then reconstruct them again. That is what I think but I dont know how to ask Matlab do these tasks.
Would you please give me a help?
Thank you very much !!

Mean of outlinks of a sparse matrix

So I'm working with sparse matrix and I have to find out different info about a very big one (10^6 size) and I need to find out the mean of the outlinks. Just to be sure I think mean is what you get from 3+4+5/3=4, 4 is the mean.
I thought of something like this:
[row,col] = find(A(:,2),1,'first')
and then I would do 1/numberInThatIndex or something similar, since it's a S-matrix (pretty sure it's called that).
And I would iterate column by column but for some reason it's not giving me the first number in each column, if I do find(A(:,1),1,'first') it does give me the first in the first column, but not in the second if I change it to A(:,2).
I'd also need something to store that index to access the value, I thought of a 2xN vector but I guess it's not the best idea. I mean, find is going to give me index, but I need the value in that index, and then store that or show it. Not sure if I'm explaining myself properly but I'm trying, sorry about that.
Just to be clear both when I input A(:,1) and A(:,2) it gives me index from the first column, and I do not want that, I want first element found from each column, so I can calculate the mean out of the number in that index.
edit: allright it seems like that indeed does work, but when I was checking the results I was putting 3817 instead of 3871 that was the given answer and so I found a 0 when I wanted something that's not a zero. Not sure if I should delete all of this.
To solve your problem, you can do the following:
numberNonZerosPerColumn = sum(S~=0,1);
meanValue = nanmean(1./numberNonZerosPerColumn);
Count the number of nonzero elements in every column n(i)
Compute the values v(i) that are stored there, which are defined by v(i) := 1/n(i)
Take the mean of those values where n(i) is not zero (i.e. summing all those values, where v(i) is not NaN and divide by the number of columns that contain at least one zero)
If you want to treat columns without any nonzero entry as v(i):= 0, but still use them in your mean, you can use:
numberNonZerosPerColumn = sum(S~=0,1);
meanValue = nansum(1./numberNonZerosPerColumn)/size(S,2);

Matlab: 'Matrix dimensions must agree' less than operator (<)?

A quick question because i fear there may already be an answer (although i cant find it)
i am getting the error: Matrix dimensions must agree.
because i am useing '<'
now with all the other operators there is a way around this either by putting '.' infront or by using a different formula. So what do people do about the less than operator????
i don't see why the greater than or equal to (>=) works but yet less than does not!?
am i being stupid and missed something really obvious??
code snippet
matrix 1 represents an array of 16 numbers
matrix 2 can represents anywhere between 10 and 20 numbers
idx = (matrix2 >= matrix1 * 0.1 & matrix2 < matrix1 * 1.5);
any help guidance or advice on the topic would be much appreciated! thank you!
EDIT
i know the matrices are different sizes but is there a way to use less then with different size arrays? as im not bothered about the size of the array but the numbers within
If you want to compare parts of matrices, like M(1:3,10:12)>A(5:7,1:3), you, probably, have to use the function squeeze():
squeeze(M(1:3,10:12))>squeeze(A(5:7,1:3))
This function remotes singleton dimensions and everything works fine after.