Inserting a cell into a matrix

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?

Risposte (2)

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 Commenti

Don
Don il 11 Lug 2011
Thanks very much for your response. I'm used to thinking of cell in the traditional sense in a spreadsheet, but in MATLAB it seems that a cell is a type of data container that can hold various complex things and is used in a cell array. If I have an n-dimensional array with integers, what do I call each of the smallest units?
For some reason when I try your code it doesn't work, even just on a=randi([10 20],7,7)
I get the error message
??? Undefined function or method 'randi' for input arguments of type 'double'.
I tried several different things with a=randi but keep getting the same message.
I've used MATLAB to write several complex programs but my knowledge is still rudimentary and I wouldn't want to waste people's time here with basic questions, can someone recommend a book that will get me up to speed in MATLAB?
Thanks!
what does
which randi
return?
Don
Don il 12 Lug 2011
I'm not sure what you're asking, I was trying the code that Paulo gave but even the first line "a=randi([10 20],7,7)" does not work and gives me the error message "??? Undefined function or method 'randi' for input arguments of type 'double'." I tried "help randi" and it gives "randi not found."
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.
randi() was not available in older versions of MATLAB. I do not recall exactly when it became available.

Accedi per commentare.

This kind of cell?
imtool(padarray(imread('cell.tif'),[20 20]))
%view cell embedded with 20 rows and columns on each side

Categorie

Scopri di più su MATLAB Coder in Centro assistenza e File Exchange

Tag

Richiesto:

Don
il 1 Lug 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by