Inserting a cell into a matrix
5 views (last 30 days)
Show older comments
Hi all, first post here, thanks for any help.
I am a new MATLAB user and am trying to insert a cell/cells into a matrix. So eg, let's say I preallocate with a=rand(7) or a=zeros(7,7). I insert my data into this predefined matrix but in my generated dataset in the 4th column the first 4 values are missing and it places a(4:7,4) in a(1:4,4). How do I insert the 4 correct values into a(1:4,4) and shift the other values down? When I try to do anything it gives me a subscripted assignment dimension mismatch because the zeros are being shifted down out of matrix and I also tried deleting the last few values in that column with a(4:7,4) = [], but it doesn't let me do so.
Alternatively, I could export it into Excel and manually make all the changes I want, but then how do I get it back into MATLAB to use in the rest of my program?
0 Comments
Answers (2)
Paulo Silva
on 1 Jul 2011
Welcome to MATLAB Answers.
Wrong use of the word cell, MATLAB cells are another class different from array.
a=zeros(7)
a(1:4,4)=4
or if you are trying to insert a vector
a=zeros(7)
v=[1 2 3 4]
a(1:4,4)=v
be sure that the number of elements in the vector match the number of positions on the array.
Now if you want to insert and shift the other values in the column down here's one way to do it
a=randi([10 20],7,7) %generate one array 7x7 with random integers from 10 to 20
v=[1 2 3 4]'; %this is the vector with the values to insert
vold=a(:,4); %save the entire column to be changed to a vector
vins=[v;vold]; %put the new values before the old values
a(1:4,4)=vins(1:4) %replace the column with the new values
5 Comments
Sean de Wolski
on 12 Jul 2011
yes, so type
which randi
at the command line, and it'll show if it's seeing the function. Since it probably isn't seeing the function - that's your issue.
Walter Roberson
on 12 Jul 2011
randi() was not available in older versions of MATLAB. I do not recall exactly when it became available.
Sean de Wolski
on 1 Jul 2011
This kind of cell?
imtool(padarray(imread('cell.tif'),[20 20]))
%view cell embedded with 20 rows and columns on each side
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!