Looping with step 10 in matlab - 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

Related

Difference between append and x = [x, element]

I've create an array X as X = [1 2 3 4 5] and I want to insert 0 at the end of it.
Is there any difference in using X = [X, 0] and X = append(X, 0)?
I didn't find anything about and I'm not sure if I can notice the difference.
Thanks in advance!
As explained in the other answer, append is part of a toolbox, and not available to everyone.
The correct way to append to a matrix, however, is
X(end+1) = 0;
This is a whole lot more efficient than X=[X,0]. The difference is that this latter form creates a new array, and copies the original one into it. The other form simply appends to the matrix, which usually doesn't require reallocation. See here for an experiment that shows the difference (read the question and my answer for both parts of the experiment).
append function is a part of Symbolic Math Toolbox. It's preferred to use [X, 0] as it is part of a core language and more likely to be understood.

Find the Start Index and End Index of the same Element in a Matrix in Matlab

I want to get the start index and end Index of the same repetitive Element in a matrix?
for example this Matrix:
the result will be:
zero is not considered
You can go this way (example for 1):
a = rem(find(A==1),5)
a(a==0)=5;
startidx = min(a)
endidx = max(a)
The same way change A==1 to whatever you need and you'll get the result. You also can create a function with parameters A, number_you_want_to_find.
There are some ways to improve this code, for example instead 5 use size(A,1) and maybe there is some way to replace all this code with one line, but this works too!
Hope, it was helpful!

Error using ' Transpose on ND array is not defined?

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

Select all elements except one in a vector

My question is very similar to this one but I can't manage exactly how to apply that answer to my problem.
I am looping through a vector with a variable k and want to select the whole vector except the single value at index k.
Any idea?
for k = 1:length(vector)
newVector = vector( exluding index k); <---- what mask should I use?
% other operations to do with the newVector
end
Another alternative without setdiff() is
vector(1:end ~= k)
vector([1:k-1 k+1:end]) will do. Depending on the other operations, there may be a better way to handle this, though.
For completeness, if you want to remove one element, you do not need to go the vector = vector([1:k-1 k+1:end]) route, you can use vector(k)=[];
Just for fun, here's an interesting way with setdiff:
vector(setdiff(1:end,k))
What's interesting about this, besides the use of setdiff, you ask? Look at the placement of end. MATLAB's end keyword translates to the last index of vector in this context, even as an argument to a function call rather than directly used with paren (vector's () operator). No need to use numel(vector). Put another way,
>> vector=1:10;
>> k=6;
>> vector(setdiff(1:end,k))
ans =
1 2 3 4 5 7 8 9 10
>> setdiff(1:end,k)
Error using setdiff (line 81)
Not enough input arguments.
That is not completely obvious IMO, but it can come in handy in many situations, so I thought I would point this out.
Very easy:
newVector = vector([1:k-1 k+1:end]);
This works even if k is the first or last element.
%create a logic vector of same size:
l=ones(size(vector))==1;
l(k)=false;
vector(l);
Another way you can do this which allows you to exclude multiple indices at once (or a single index... basically it's robust to allow either) is:
newVector = oldVector(~ismember(1:end,k))
Works just like setdiff really, but builds a logical mask instead of a list of explicit indices.

Replace certain elements of matrix with NaN (MATLAB)

I have a vector, A.
A=[3,4,5,2,2,4;2,3,4,5,3,4;2,4,3,2,3,1;1,2,3,2,3,4]
Some of the records in A must be replaced by NaN values, as they are inaccurate.
I have created vector rowid, which records the last value that must be kept after which the existing values must be swapped to NaN.
rowid=[4,5,4,3]
So the vector I wish to create, B, would look as follows:
B=[3,4,5,2,NaN,NaN;2,3,4,5,3,NaN;2,4,3,2,NaN,NaN;1,2,3,NaN,NaN,NaN]
I am at a loss as to how to do this. I have tried to use
A(:,rowid:end)
to start selecting out the data from vector A. I am expecting to be able to use sub2ind or some sort of idx to do this, possibly an if loop, but I don't know where to start and cannot find an appropriate similar question to base my thoughts on!
I would very much appreciate any tips/pointers, many thanks
If you are not yet an expert of matlab, I would stick to simple for-loops for now:
B = A;
for i=1:length(rowid)
B(i, rowid(i)+1:end) = NaN;
end
It is always a sport to write this as a one-liner (see Mohsen's answer), but in many cases an explicit for-loop is much clearer.
A compact one is:
B = A;
B(bsxfun(#lt, rowid.', 1:size(A,2)))=NaN;