reshape() command causes an error being evoked from a loop - matlab

psz=length(pic)
p=0; %masking counter
for i=1:outs:(psz) % dividing in blocks
for j=1:outs:(psz)
p=p+1
blocks(:,:,p)=pic(i:i+outs-1,j:j+outs-1);
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';
end
end
so to begin with i am trying to reproduce sanger rule for pca using neural networks so if somone wants to duscuss about it or give him my code he can message me:)
i get the following error
Error using reshape
To RESHAPE the number of elements must not change.
Error in train (line 30)
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';

Converting blocks(:,:,p) to a column vector should clear the error as long as there are the same number of elements in blocks(:,:,p) as the row length of ins
col_vec = blocks(:,:,p);
ins(:,p) = col_vec(:);

The size of blocks(:,:,p) is outs-by-outs so to make a column vector, it has to be (outs*outs)-by-1. To do this, the command would be:
ins(:,p)=reshape(blocks(:,:,p)',outs*outs,1); % no need for '
However, be sure that size(ins,1) is outs*outs or it won't work. What is the size of ins (and blocks, out of curiosity)? Also, be sure you really want the ' on blocks because the command will work with or without it.

Related

“Index exceeds the number of array elements” for piecewise function

I am trying to create piecewise functions V(t), D(t).
I try to find the piecewise, then I use the piecewise of t to construct functions and plot them.
But it shows "Index exceeds the number of array elements. Index must not exceed 51".
How can I fix it?
I put my code below and I really hope someone can answer it. Thaks!
z=zeros(1,50);
p_i=zeros(1);
p=0.023;
for i=1:50
z(i)=rand;
if z(i)>p
p_i(end+1)=i+z(i);
end
end
n=numel(p_i);
V=zeros(1,n);
w=zeros(1,n);
D=zeros(1,n);
V_op=zeros(1,n);
%get the number of pi
sigma_w0=0.2;
Q=5;
P=2;
Q_op=4;
for i=1:n
if i>1
w(i)=w(i-1)+normrnd(0,sigma_w0);
V(i)=Q*w(i);
D(i)=P*w(i);
V_op(i)=Q_op*w(i);
else
w(1)=2;
V(i)=Q*w(i);
D(i)=P*w(i);
V_op(i)=Q_op*w(i);
end
end
t=0:0.0002:50;
V_p=zeros(size(t));
D_p=zeros(size(t));
V_opp=zeros(size(t));
for m=1:length(t)
t(m)>=p_i(i)& t(m)<p_i(i+1)
V_p(m)=V(i);
D_p(m)=D(i);
V_opp(m)=V_op(i);
end
Yes, if you run your code you'll see it is functional before the last for loop since it is evaluating that in the 1 to 50 range (m=1:length(t)) but your line is printing 51 values so you need to check only the next part and reorganize the idea:
t(m)>=p_i(i)& t(m)<p_i(i+1)
If you print the first part (t(m)>=p_i(i)) it is okay, but check the other part and you'll notice the error. Maybe you can print all your results moving your increment value (+1) and prevent it from exceeding 1 to 51.

Dimension mismatch, while adding columns to existing Matlab matrix

I am looking for a tip to feed extra columns of px matrix with data, as my code ends up with Error: Subscripted assignment dimension mismatch from z=2. ps: While breaking the code it only works fine for z=1. I've also tried
px(:,z:ind)=getsqldata(data{z,3}) as a way around but without success.
ind=20;
T=250; % time period
z=1; %initialization
for i=z:ind
while (data{z,6}~='C' ||data{i,6}~='OT')
px(:,z)=getsqldata(data{z,3});
if px(1,1)=='No Data'
error('test');
end
ret(:,z) = -price2ret(px(:,z));
ret=ret(1:T,1);
z=z+1;
end
end
Best,
ps: getsqldata() is my user-defined function, retrieving data from database. I can confirm that there is nothing wrong with it, as I've used it for a while.

Looping over a Matlab structure

I am currently processing a bunch of files that I have imported into a structure, but have hit a bump in the road while trying to loop over the data.
First of all, here is my structure:
Ice
1.1 az160, az240, az300...
1.1.2 zen15, zen30,zen45...
1.1.2.1 Data
1.1.2.2 Textdata
I am trying to extract a value from each "textdata" cell array and use it to divide a column in data of the same structure. To do so, I am looping through the structure in the following way:
az_names = fieldnames(ice)
for m = 1:numel(az_names)
snames = fieldnames(ice.(az_names{m}))
for k = 1:numel(snames)
inttime = strrep(ice.(az_names{m}).(snames{k}).textdata(9,1), 'Integration > Time (usec): ','');
inttime = strrep(inttime, ' (USB2+E00040)','');
integration = cellfun(#str2num,inttime)
line 17 ice.(az_names{m}).(snames{k}).data(:,4) = ice.(az_names{m}).(snames{k}).data(:,3)/integration
end
end
I get the following error:
Index exceeds matrix dimensions
Edit: Matlab gives me the error at line 17. If I run the code up to "integration" and also write:
ice.(az_names{m}).(snames{k}).data(:,4)
I don't get a problem, Matlab prints to screen the right number and the data column.
I thought this would loop through each field in the structure and do the operation (dividing a column of values by a number), but I seem to be missing a point here. Can anybody see my mistake?
Regards,
If the error occurs when you try to execute this fragment:
ice.(az_names{m}).(snames{k}).data(:,4)
Then the cause is quite simple.
The variables m and k seem to be handled properly (due to the numel in the loop they should never be too big), meaning that the 4 is simply too big.
Check
ice.(az_names{m}).(snames{k}).data(:,4)
Or even more directly
size(ice.(az_names{m}).(snames{k}).data)
And you should find that the second element of the size is less than 4.
On second thought, this fragment works:
a.b=1;
a.b(:,4)=1
So I suspect that the error occurs when trying to read in this part:
ice.(az_names{m}). (snames{k}).data(:,3)
Meaning that the second element of the size should even be less than 3.
Also I would recommend removing the space.

(simple?) error with findpeaks and if loop

I'm trying to find the local maxima of a data set using the findpeaks() function and so far I have this code:
[pks, locs] = findpeaks(signal);
max_times = zeros(size(locs));
if n = 1:size(locs);
max_times(n) = (times(locs(n)));
end
What am I trying to do? Well I have a set of signal data and the corresponding times. I want to get the local maxima values and output two vectors; the maxima signal values and the times that they occured.
How am I doing it? I'm using the findpeaks function to find the peaks (pks) and location (locs) of the maxima. I'm then setting up a blank array the same length as the locs vector and then using an if loop to fill up the empty max_times(n) vector with the times that the maxima occur
The problem? I keep getting this errorExpression or statement is incomplete or incorrect. about my if loop. I don't understand what this means/ how do I solve this problem/ edit my code to get it to do what I want?
Thanks for any help!
What you are thinking is totally wrong.
If is not a loop its a conditional statement
what you want here is a for loop
for n = 1:size(locs)
% your code
end
also times take two parameter and you should figure it out yourself what it should be

error message on the dimensions of matrix in Matlab

When I run this program, I get the error message:
??? Index exceeds matrix dimensions.
Error in ==> if a(1,i)==0
could you tell me why??
a = randi(5,4,100)-ones(4,100);
[n m]=size(a);
for i=1:m
if a(1,i)==0
a(:,i)=[];
end
end
The reason is that you are removing columns from your matrix, so inside the for loop you are reducing its dimension. Then you try to access a column with an index which refers to the original matrix, before the columns were removed.
Try this instead:
a = randi(5,4,100)-1;
ind2remove = (a(1,:) == 0);
a(:,ind2remove) = [];
You get that error because during the execution of this for loop, you might remove some columns. Therefore the dimensions of the matrix will decrease and you will try to access elements that have been moved to a different place.
To do wht you want, you either have to write a while loop, keeping the indices in check manually. The other solution is to vectorize your solution as itamar Katz has shown. That solution is more MATLAB-esque than writig a while loop.
But I have noticed that allowing a random algorithm to emit vectors of random length can sometimes prove more difficult to handle than fixed-length vectors. So you might want to construct your vector in such a way that you don't even have to remove such entries, depending on your application this might be accomplished by generating the first row and other rows with different instructions.