matlab reshape function - error on number of elements - matlab

Hi I have the following code
A = squeeze(hourly_surplus(1,1,1,:));
B = reshape(A,365,24);
Where size(A) = 8760 x 1
however I get the error
Error using reshape
To RESHAPE the number of elements must not change
This error appears on the line of "B", however I think that A has 8760 elements and B = 365 x 24 which also is 8760. What could be going wrong?
Thanks

This:
A=rand(8760,1);
B=reshape(A,365,24);
works fine, so the problem is with your A.

Related

Trying to develop closed form solutions of second order DE

Basically, I have this function in matlab:
function yy = homo2nd(tt,polycc,y0,v0)
rr = roots(polycc);
cc = [1 1; rr(1) rr(2)\[y0; v0]];
yy = cc(1)*exp(rr(1)*tt) + cc(2) * exp(rr(2)*tt);
yy = round(yy,8);
end
I understand that roots takes the roots of an array signifying the coefficients of the left side of the equation. I feed roots and argument like [1 1 1] and it spits out the eigenvalues of that function.
From there I am lost.
I define tt in the command shell like this:
tt = linspace(0,2*pi,100).
I call homo2nd and feed it arguments (tt,[1 1 1], 1, 1)
When I try to feed roots the argument [1 1 1] like I said I get this error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
I just need help interpreting the code. Why are the arrays horizontally mismatched?
The error is occuring on this line
cc = [1 1; rr(1) rr(2)\[y0; v0]];
So let's look at this, the 1 1 is fine, it's saying there's a row vector [1,1].
The next part is where the error occurs, the ; moves us to a second row. The first element of that row is rr(1), this is fine.
However, the second element rr(2)\[y0; v0] is using the mldivide command and the result of this isn't a single element, hence the error.

Assigning empty matrix to empty sub-matrix in Matlab

When running following code in Matlab:
a = magic(3);
b = [];
a([],:) = [] % works
a([],:) = b % doesn't work
I get an error when using variable 'b':
>> tmp
a =
8 1 6
3 5 7
4 9 2
Subscripted assignment dimension mismatch.
Error in tmp (line 5)
a([],:) = b
Does anyone know what's going on here? Why one assignment works and the other doesn't?
Here's my guess at why Matlab behaves like that:
I think that assigning to [] can be considered a special operator, i.e. a(1,:) = [] will essentially delete the first row of a. So even though size(a(1,:)) differs from size([]), I reckon the Matlab interpreter knows that this special case is not an assignment.
However a(1,:) = b when b=[] will give you a subscript dimension mismatch. I think this is correct behaviour because in this case you are assigning, and you're trying to assign a 0-by-0 to a 1-by-3 which is a dimension mismatch. The same goes for a([],:) = b, you're trying to fit a 0-by-0 into a 0-by-3 space which is again a mismatch.
So in conclusion, the second case is an assignment operator and so the error makes sense. The first case is a special delete operator and hence no error.
I have no references for any of this (this is all I could find in the docs but it doesn't really cover everything)
However I don't think this explains all the behaviour, some examples brought up from the comments:
Assume:
a = magic(3);
a2 = magic(4);
b = [];
a([],:) + a2([],:) gives a dimension mismatch error as expected.
a([],:) = a2([],:) does not throw an error... which to me is not expected
a([],:) = b(:) also does NOT throw an error... which is again quite strange, unless we can assume that the (:) operation returns a comma separated list like the {:} does (although I know that is not the case)???
These cases seem inconsistent to me.
We can expand on case 2:
a([],:) = zeros(0,0)
a([],:) = zeros(0,2)
a([],:) = zeros(0,3)
a([],:) = zeros(0,4)
only he first case throws an error and the other 3 are accepted by Matlab. Looks like this answer is just creating further questions :/

Why does my Matlab Random Shuffle not work?

I tried figuring this out but it doesn't work.
Say I have
a = [5,1,5,6,7,2,4];
Now, if I do a(randperm(a)) i get :
ans =
1 7 6 5 5
Now, I also have
b = [1,2,3,4,5,6,7,8,9,10];
However, if I do b(randperm(b)) :
ans =
1
Why is this happening? This does not seem to make any sense to me. How do I shuffle a vector like :
z = [1,2,3,4.... 1500,6001,6002,6003... 8999];
randperm takes a single N and returns a permutation of the numbers 1 to N.
I don't have Matlab installed so I can't try this out, but this should work:
a(randperm(length(a)))
The parameter of randperm should not be the array but it's length.
Try
B(randperm(length(B)))

Cumulative Sum of a Vector - Syntax

I am trying to resolve why the following Matlab syntax does not work.
I have an array
A = [2 3 4 5 8 9...]
I wish to create an indexed cumulative, for example
s(1) = 2; s(2)=5, s(3)=9; ... and so on
Can someone please explain why the following does not work
x = 1:10
s(x) = sum(A(1:x))
The logic is that if a vector is created for s using x, why would not the sum function behave the same way? The above returns just the first element (2) for all x.
For calculating the cumulative sum, you should be using cumsum:
>> A = [2 3 4 5 8 9]
A =
2 3 4 5 8 9
>> cumsum(A)
ans =
2 5 9 14 22 31
The issue is that 1:x is 1 and that sum reduces linear arrays. To do this properly, you need a 2d array and then sum the rows:
s(x)=sum(triu(repmat(A,[prod(size(A)) 1])'))
You are asking two questions, really. One is - how do I compute the cumulative sum. #SouldEc's answer already shows how the cumsum function does that. Your other question is
Can someone please explain why the following does not work
x = 1:10
s(x) = sum(A(1:x))
It is reasonable - you think that the vector expansion should turn
1:x
into
1:1
1:2
1:3
1:4
etc. But in fact the arguments on either side of the colon operator must be scalars - they cannot be vectors themselves. I'm surprised that you say Matlab isn't throwing an error with your two lines of code - I would have expected that it would (I just tested this on Freemat, and it complained...)
So the more interesting question is - how would you create those vectors (if you didn't know about / want to use cumsum)?
Here, we could use arrayfun. It evaluates a function with an array as input element-by-element; this can be useful for a situation like this. So if we write
x = 1:10;
s = arrayfun(#(n)sum(A(1:n)), x);
This will loop over all values of x, substitute them into the function sum(A(1:n)), and voila - your problem is solved.
But really - the right answer is "use cumsum()"...
Actually what you are doing is
s(1:10)= sum(A(1:[1,2,3...10]))
what you should do is
for i=1:10
s(i)=sum(A(1:i))
end
hope it will help you

Removing consecutive duplicates from vector MATLAB

Here is my MATLAB code:
[numeric, pics] = xlsread('matrix.xls');
[r,c] = size(pics);
done = r*c;
randvecall = randsample(done, done, true);
randvec = randvecall([1,diff(randvecall)]~=0);
currk = randvec(k);
Essentially what this does is it builds an array of values from a Microsoft Excel spreadsheet. I want to have duplicates in the array, but not consecutive duplicates, so I added a line of code that removes them. When I manually enter values into randvecall and run the above code, it works perfectly. However, when I run the code as seen above, I get the following error:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> testAS_randsample at 76
randvec = randvecall([1,diff(randvecall)]~=0);
Why is this happening? For example, this works:
randvecall=[1 2 3 4 5 5 5 5 8 7 8 8];
randvec = randvecall([1,diff(randvecall)]~=0);
disp(randvec)
randvec = [1 2 3 4 5 8 7 8]
That is exactly what I want my code to do. But why does my actual code give me the horzcat error message? Can anybody help me with this? It must have something to do with the way randsample is building the randvecall array, but I can't figure out why this would give me that error message?
That seems to be a problem with how randsample(n,k,true) works: it returns a 1xk vector, while you need a kx1 vector. Transposing randvecall should do the trick.
EDIT:
Let me rephrase it in code for the general reader:
randvec = randvecall([1,diff(randvecall')]~=0);