Inserting a cell into a matrix

조회 수: 5 (최근 30일)
Don
Don 2011년 7월 1일
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?

답변 (2개)

Paulo Silva
Paulo Silva 2011년 7월 1일
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
Sean de Wolski
Sean de Wolski 2011년 7월 12일
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
Walter Roberson 2011년 7월 12일
randi() was not available in older versions of MATLAB. I do not recall exactly when it became available.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2011년 7월 1일
This kind of cell?
imtool(padarray(imread('cell.tif'),[20 20]))
%view cell embedded with 20 rows and columns on each side

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by