Error using ' Transpose on ND array is not defined? - matlab

I am getting error for my below code: temp=reshape(img',irow*icol,1);
Error message:Error using '
Transpose on ND array is not defined.
What is solution for this. I think I have to use permute(A,order) command. But I dont know how to use this command in my code. Do you know any solution?
for i=1:M
str=strcat(int2str(i),'.jpg'); %concatenates two strings that form the name of the image
eval('img=imread(str);');
subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
imshow(img)
if i==3
title('Training set','fontsize',18)
end
drawnow;
[irow icol]=size(img); % get the number of rows (N1) and columns (N2)
temp=reshape(img',irow*icol,1); %creates a (N1*N2)x1 matrix
S=[S temp]; %X is a N1*N2xM matrix after finishing the sequence
%this is our S
end

I assume the code was designed for grey scale images. For matrices with more than two dimensions, you have to use permute. One solution could be:
[irow icol d]=size(img);
temp=reshape(permute(img,[2,1,3]),[irow*icol,d]);
Which results in a nx3 matrix, each column corresponding to one colour. You have to change the last line as well, but I don't know what you are expecting. Maybe take a look at cat

Related

Reshape structure into a matrix without for loop

I have a structure (data) which consist of 322 cells a 296(features)*2000(timepoints). I want a matrix per timepoint which consists of trials^features^timepoints (322 *296*2000). What I am currently doing and what also works fine is using a for-loop:
for k=1:size(data.trial{1,1},2)
for i= 1:length(data.trialinfo)
between=data.trial{1,i}';
data(i,:,k)=between(k,:);
end
end
Can anyone think of a faster way to do that? Because it takes ages as the matrix increases.
Thanks!
Carlos
Try:
data2 = permute(reshape([data.trial{:}],296,2000,322),[3,1,2]);
The issue with reshape is that it thinks in columns, so you need to permute afterwords (ie. 3D transpose), since that's how you set up the output variable in your loop. I used concatenation instead of cell2mat for speed.
I assumed this worked as sample data:
for ii= 1:322
data.trial{1,ii} = rand(296,2000);
end

Error message when storing output from loop in matrix

I have this program which calculates the realized covariance for each day in my sample but I have some troubles with storing the output in a matrix.
the program is as follows:
for i=1:66:(2071*66)
vec = realized_covariance(datapa(i:i+65),data(i:i+65),datapo(i:i+65),data(i:i+65),'wall','Fixed',fixedInterval,5)
mat(2,4142) = vec
end
Output:
vec =
1.0e-03 *
0.1353 -0.0283
-0.0283 0.0185
Subscripted assignment dimension mismatch.
I have tried various way to store the output in a matrix like defining a matrix on zeroes to store the output in or let the row dimension of the storing matrix be undefined, but nothing seems to do the job.
I would really appreciate an advice on how to tackle this challenge.
I have used a solution which does the job.
I defined a matrix and then filled in all my output one at the time using the following:
A = zeros(0,0) %before loop, only serve to define the storing matrix
A = [A; vec]%after the calculating function, inside the loop.
Actually mat(2,4142) is a single location in a matrix, you can't assign there four values.
You need to define the exact location inside mat every time you want to assign values into it. Try doing it like that:
mat=zeros(2,2142);
for k=1:66:(2071*66)
vec=realized_covariance(datapa(i:i+65),data(i:i+65),datapo(i:i+65),data(i:i+65),'wall','Fixed',fixedInterval,5)
mat(:,[(((k-1)/66)*2)+1 (((k-1)/66)*2)+2])=vec;
end
You're trying to store a 2 x 2 matrix into a single element. I.e. 4 elements on the right hand side, one on the left. That won't fit. See it like this: you have a garage besides your house where 1 car fits. You've got three friends coming over and they also want to park their car inside. That's a problem though, as you've got only space for one. So you have to buy a bigger garage: assign 4 elements on the left (e.g. mat(ii:ii+1,jj:jj+1) = [1 2;3 4]), or use a cell/structure array.
As Steve suggests in a comment below, you can use a 3D matrix quite easily:
counters = 1:66:(2071*66);
mat = zeros(2,2,numel(counters)); %// initialise output matrix
for ii=1:numel(counters)
vec = realized_covariance(datapa(counters(ii):counters(ii+65)),...
data(counters(ii):counters(ii+65)),datapo(counters(ii):counters(ii+65)),...
data(counters(ii):counters(ii+65)),'wall','Fixed',fixedInterval,5)
mat(:,:,ii) = vec; %// store in a 3D matrix
end
Now mat is 3D, with the first two coordinates being your regular output, i.e.e vec, and the last index is the iteration number. So to access the output of iteration 1032 you'd do mat(:,:,1032), possibly with a squeeze around that to make it 2D instead of 3D.

Append legends. Whats wrong with my code?

I'm trying to add legends the plot when it adds a curve. I can't see whats wrong with my code, can someone please help? I'm using matlab r2015a on ubuntu.
x=1:5;
v=1:5;
plot(x,v)
[~,~,plots,str] = legend('1');
hold on
for i=4:10
pl=plot(x,v*i);
[~,~,plots,str]=legend([plots;pl],str,num2str(i))
end
when i run it i get:
plots =
1x2 Line array:
Line Line
str =
'1' '4'
Error using vertcat
Dimensions of matrices being
concatenated are not
consistent.
So its means it works the first lap but not the second.
As discussed in the comment, the way Matlab handles legends is different than in earlier releases; they are now legend objects.
In any case, to solve your problem, which is a dimension mismatch problem, you can simply concatenate vertically the outputs you get using the transpose operator, because Matlab returns a horizontal array of line and text objects whereas you want a vertical array. Therefore, using plots.' and pl.' works fine. Also, it's good practice not to use i as a loop counter since it represents the imaginary unit.
clear
clc
close all
x=1:5;
v=1:5;
plot(x,v)
[~,~,plots,str] = legend('1');
hold on
for k=4:10
pl=plot(x,v*k);
[LegendObject,~,plots,str]=legend([plots.';pl.'],str,num2str(k));
end
%// Use the legend object to modify its properties/location
set(LegendObject,'Location','NorthWest');
Output:

Looping with step 10 in matlab

I run the below code to get the matrix a filled with the values from 0 to 2062630 alternatively with step that is. a(1) should be 0, a(2) 10 etc. or simply a should contain 0,10,20,30,40,......,2062630. But insted the code gives the matria with the value 2062630 in each element of the matrix a.
for i=1:length(x)
for j=0:10:2062637
a(i,:)=j;
end
end
I think you did not understand how the colon operator works, it already generates the matrix you want.
a=0:10:2062637
It's not quite clear what you want your code to produce, but you may not need any for loops. Instead you can use repmat:
a = repmat(0:10:2062637,[length(x) 1]);
size(a)
This will create a matrix a with length(x) rows, each of which is 0:10:2062637. It's also possible that you're also trying to create the transpose of this:
a = repmat((0:10:2062637).',[1 length(x)]);
size(a)
I'm not sure what you want, if you want a vector or a matrix? Also I don't know what x is.
You could try:
count=1;
for j=0:10:2062637
a(count)=j;
count=count+1;
end
Which returns exactly the same thing as the solution proposed by #Daniel:
a=0:10:2062637

Inner matrix dimensions must agree error

I have a piece of code in which i save array values to a .txt file and then in another function i have to retrieve those values from .txt to an array...the code looks somewhat like this...
fid = fopen('c:\\coeffs2.txt','wt');
fprintf(fid,'%f\n',descr2);
fclose(fid);
And in another file i retrieve it this way..
fid = fopen('c:\\coeffs2.txt');
des2= [];
des2 = fscanf(fid,'%f\n');
fclose(fid);
i get the error as inner matrix dimension must agree...please help!
Are you sure these lines are the ones generating that error? Exactly what is the line where the error occurs? Normally this would happen if you did (for example) a matrix multiplication (*) when you intended to do element-by-element multiplication (.*) with a non square matrix...
You can use save('c:\\coeffs2.mat', 'descr2'); and load('c:\\coeffs2.mat'); as an alternative (and more efficient) way to store / retrieve the matrix, and be sure you didn't change the dimensions.
Did you try to see what size(descr2) gives before the save, and after retrieving? Maybe you just need a resize...