How can I delete the rows and columns of a 3D matrix? I tried to use the following Px((1:(Column1-1)),:,i)=[] but I am getting this error "A null assignment can have only one non-colon index." How can I correct it?
조회 수: 5 (최근 30일)
이전 댓글 표시
clear all
clc
dicomlist = dir(fullfile(pwd,'SER_7_Dicoms','*.dcm'));
for cnt = 1 : numel(dicomlist)
I{cnt} = dicomread(fullfile(pwd,'SER_7_Dicoms',dicomlist(cnt).name));
% Each{cnt} = char(dicomlist(cnt).name)
% %info = dicominfo('MR03318.dcm');
% %dicominfo(dicomlist(cnt).name)
end
for cnt = 1 : numel(dicomlist)
info{cnt} = dicominfo(fullfile(pwd,'SER_7_Dicoms',dicomlist(cnt).name));
end
for i = 1:length(info)
Rows(i) = double(info{1,i}.Rows);
Columns(i) = double(info{1,i}.Columns);
Pixel_Spacing(:,i) = double(info{1,i}.PixelSpacing); %in mm/pixel
Pixel_Spacing_x(:,i) = double(info{1,i}.PixelSpacing(1));
Pixel_Spacing_y(:,i) = double(info{1,i}.PixelSpacing(2));
Patient_Position(:,i) = double(info{1,i}.ImagePositionPatient);
Sx(:,i) = double(info{1,i}.ImagePositionPatient(1));
Sy(:,i) = double(info{1,i}.ImagePositionPatient(2));
Sz(:,i) = double(info{1,i}.ImagePositionPatient(3));
Image_Orientation(:,i) = double(info{1,i}.ImageOrientationPatient);
F12(:,i) = double(Image_Orientation(4,i));
F22(:,i) = double(Image_Orientation(5,i));
F32(:,i) = double(Image_Orientation(6,i));
F11(:,i) = double(Image_Orientation(1,i));
F21(:,i) = double(Image_Orientation(2,i));
F31(:,i) = double(Image_Orientation(3,i));
F1(:,i) = [F11(:,i) F21(:,i) F31(:,i)];
F2(:,i) = [F12(:,i) F22(:,i) F32(:,i)];
n(:,i) = cross(F1(:,i),F2(:,i))%vector normal to the plane
magnitude(:,i) = sqrt(n(1,i)^2+n(2,i)^2+n(3,i)^2);
n_normalized(:,i) = n(:,i)/magnitude(:,i);
V1 = 202.0;
V2 = 220.0;
H1 = 80.0;
H2 = 120.0;
Row1(:,i) = round(V1/Pixel_Spacing_y(:,i));
Row2(:,i) = round(V2/Pixel_Spacing_y(:,i));
Column1(:,i) = round(H1/Pixel_Spacing_x(:,i));
Column2(:,i) = round(H2/Pixel_Spacing_x(:,i));
Delta_r_ROI(:,i) = double(Pixel_Spacing_x(:,i));
Delta_c_ROI(:,i) = double(Pixel_Spacing_y(:,i));
%fileID(i) = fopen(sprintf('New%d.dat',i),'w');
A(:,:,i) = [(F11(:,i)*Delta_r_ROI(:,i)) (F12(:,i)*Delta_c_ROI(:,i)) 0 Sx(:,i); F21(:,i)*Delta_r_ROI(:,i) F22(:,i)*Delta_c_ROI(:,i) 0 Sy(:,i); F31(:,i)*Delta_r_ROI(:,i) F32(:,i)*Delta_c_ROI(:,i) 0 Sz(:,i);0 0 0 1];
subImage(:,:,i) = I{1,i}(Row1(:,i):Row2(:,i), Column1(:,i):Column2(:,i));
%figure(i), imshow(subImage(:,:))
for r = (Row1(:,i)):(Row2(:,i))
for c = Column1(:,i):Column2(:,i)
B(:,i) = [r;c;0.0;1.0];
P(:,:,i) = A(:,:,i)*B(:,i);
Px(c,r,i)=P(1,:,i);
Py(c,r,i)=P(2,:,i);
Pz(c,r,i)=P(3,:,i);
%disp([r c])
end
end
Px((1:(Column1-1)),:,i)=[];
Py(1:(Column1-1),:,i)=[];
Pz(1:(Column1-1),:,i)=[];
Px(:,1:(Row1-1),i)=[];
Py(:,1:(Row1-1),i)=[];
Pz(:,1:(Row1-1),i)=[];
댓글 수: 0
답변 (2개)
the cyclist
2016년 5월 24일
Suppose you have the 2-dimensional matrix
A = [1 4 7;
2 5 8;
3 6 9]
and try the command
A(3,3) = []
You are telling MATLAB to remove the 9. But that makes no sense. What is left is not a matrix. You could insert some other value there, such as
A(3,3) = NaN
but you cannot just remove it.
You are trying to do the same thing in your array.
Image Analyst
2016년 5월 24일
You have to erase a whole plane, not just some columns.
댓글 수: 2
Walter Roberson
2016년 5월 24일
Px((1:(Column1-1)),:,:)=[]
You might wanting to loop over the third dimension, but MATLAB does not know that Column1 is not going to change on the other panes and so does not know that "eventually" it would not be a "ragged array". And if you know that Column1 is not going to change then just do it all at once like the above.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!