matlab: "Index of element to remove exceeds matrix dimensions." When I am not removing any elemens - matlab

I get the error
??? Index of element to remove exceeds matrix dimensions.
Error in ==> myfile at 111
C(i)=s{i,3};
the code being:
C=zeros(num_of_tris,1);
for i=1:size(C,1)
C(i)=s{i,3};
end
I'm not showing the code for creating s, but I assume it's beside the point as s only appears on the right hand side of the assignment...
why does it say element to remove? which element am I removing?

Ok, so here's what is happening. s is probably initialized to an empty cell (NOTE: need not be entirely empty -- see last paragraph). So, indexing an element of s as s{i,3} returns []. The MATLAB operation to remove an element of a vector is
C(i)=[];
So when you loop through, you're removing the elements of C one by one, and eventually, the index i exceeds the size of the (now diminished) vector.
Here's a small example that reproduces your problem:
s=cell(10,5); %#initialize s to an empty cell
%#note that any cell returns []
s{3,4}
ans =
[]
%#This is your code from above
C=zeros(10,1); %#initialize C
for i=1:size(C,1)
C(i)=s{i,3};
end
??? Index of element to remove exceeds matrix dimensions.
You'll find that the index i when you get this error is numel(C)/2+1. In other words, till i=5 (in this example), you're removing every odd element of C and at i=6, the number of elements remaining in C is 5, and so you get an index out of bounds error.
NOTE:
s need not even be entirely empty. Consider this example:
s=cell(10,1);
s([1,2,6,8])=num2cell(rand(4,1));
C=zeros(10,1);
for i=1:numel(C)
C(i)=s{i};
end
??? Index of element to remove exceeds matrix dimensions.

Related

Reform matrix from a 2D logical matrix (without reshaping)

I tried to rebuild the matrix from the logical arguments as in the example below:
a=rand(2,5)
b=rand(2,5)
c=a>b
a(:,c)=b(:,c)
However I get Index exceeds matrix dimension error. Can this be done without reshaping the matrix beforehand?
If you want to copy the least value between, a and b into a for each entry try:
a=rand(2,5);
b=rand(2,5);
c = find(a>b); % c contains the position of value of the greatest val
a(c)=b(c) % removes greates value copying a smaller value from b
If you want to copy the greatest value modify the statement creating variable c.
Index exceeds matrix dimensions is caused by you trying to use as matrix dimensions a list : and a matrix c. Find creates a list of values which satisfy the relationship wanted and can be used to recall specific values.

Copy matrix rows matlab

Lets say i have a matrix A of 300x65. the last column(65th) contains ordered values (1,2,3). the first 102 elements are '1', the second 50 elements are '2' and the remainder will be '3'.
I have another matrix B, which is 3x65 and i want to copy the first row of B by the number of '1's in matrix A. The second row of B should be copied by the number of '2's in in matrix A and the 3th row should be copied by the remaining value of matrix A. By doing this, matrix B should result in a 300x65 matrix.
I've tried to use the repmat function of matlab with no succes, does anyone know how to do this?
There are many inconsistencies in your problem
first if you copy 1 row of B for every element of A(which will end up happening by your description) that will result in a matrix 19500x65
secondly copy its self is a vague term, do you mean duplicate? do you want to store the copied value into a new var?
what I gathered from your problem is you want to preform some operation between A and B to create a matrix and store it in B which in itself will cause the process to warp as it goes if you do not have another variable to store the result in
so i suggest using a third variable c to store the result in and then if you need it to be in b set b = C
also for whatever process you badly described I recommend learning to use a 'for' loop effectively because it seems like that is what you would need to use
syntax for 'for' loop
for i = [start:increment:end]
//loops for the length of [start:increment:end]
//sets i to the nth element of [start:increment:end] where n is the number of times the loop has run
end
If I understand your question, this should do it
index = A(:,end); % will be a column of numbers with values of 1, 2, or 3
newB = B(index,:); % B has 3 rows, which are copied as required by "index"
This should result in newB having the same number of rows as A and the same number of columns as the original B

Remove elements at a set of indices in a multidimensional array in MATLAB?

I have a multidimensional array A with 1000 elements (1000x3). I have another vector with index positions of elements I want to remove from this array.
I've tried using this A(indices) = [] or A(indices,:,:) = [], but the problem is that the result changes A's dimension, so if indices has 10 elements, I find A's size become 2990x1 instead of 990x3. Anyone can advise how to remove the elements having the indices in A where A's dimensions won't change will still be n x 3?
You can use logical indexing to filter the matrix, for example,
A=rand(1000,3);
A(A(:,1)>0.9)=[];
which removes the rows of A that have a value greater than 0.9 in the first column.
I'm not sure why your original approach didn't work though.

How to take the mean of columns from an array within an array?

I am currently working on a project in Matlab where I have a cell array of cell arrays. The first cell array is 464 columns long and 1 row deep. Each of these cells is another cell array that is 96 columns and 365 rows. I need to be able to get the mean of the 96 columns for each of the 464 arrays and place each of the 464 arrays on a different row in a new array called mean. I have tried to write code to just do one column as follow:
mean = Homes{1,1}(1:)
But I when ever I try to run this code I got the follow error:
mean = Homes{1,1}(1:)
|
Error: Unbalanced or unexpected parenthesis or bracket.
Basically my final array name mean needs to be 96 columns by 464 rows. I am stuck and could really use your help.
Thank you.
I suggest you to try the following code on a smaller matrix. See if it gives you the desired results.
a=cell(1,4); %for you it will be a=cell(1,464)
for i=1:4
a{i}=randi(10,[5 10]); %for you it will be a{i}=randi(10,[365 96]);
end
a1=cell2mat(a); %just concatenating
a1=mean(a1); %getting the mean for each column. in your case, you should get the mean for 96*464
a2=reshape(a1,[10 4]); %now what reshape does it it takes first 10 elements and arranges it into first column.
%Therefore, since I finally want a 4x10 matrix (in your case 464x96), i.e. mean of 10 elements in first cell array on first row and so on...
%Thus, 1st 10 elements should go to first column after doing reshape (since you want to keep them together). Therefore, instead of directly making matrix as 4x10, first make it as 10x4 and then take transpose (which is the next step).
a2=a2'; %thus you get a 4x10 matrix.
In your case specifically, the code will be
a=cell(1,464);
for i=1:464
a{i}=randi(10,[365 96]);
end
a1=cell2mat(a);
a1=mean(a1);
a2=reshape(a1,[96 365]);
a2=a2';

how can i deal with empty matrix: 0-by-any number resulting from simulation?

I have made a simulation and the result each time of the simulation is a matrix and i choose a certain row from the matrix, so if the simulation run=500, i'll have a 500 matrix and ,the row i choose each time will be (at the end of the simulation) 500 rows [one row from the first matrix...last row from the last matrix]...
the problem is some times a matrix dose not contain the certain row i want , the answer is for example empty matrix: 0-by-6
i want to ignor this answer
Note: the row i choose is not necessary to be exist in all matrices
so if the run=600 , result in 600 matrix , the row i choose maybe =400 only and the other 200 will be zero
the simulation STOP when the result is empty matrix: 0-by-any number
I use Matlab
you can use isempty to detect empty arrays, for example
a=zeros(0,5)
isempty(a)
a =
Empty matrix: 0-by-5
ans =
1
For when the index exceeds matrix dimensions, you can add a condition that tests the size of your matrix, specifically, how man rows using size(m,1)
So all together, in your for loop you can code something like:
for n=1:blah
if ~isempty(M) % continue if matrix is non-empty
if size(M,1)<=n % continue if index doesn't exceeds matrix dimensions
....
....