Unsatisfied answer and no errors
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hege
el 26 de Jun. de 2020
Comentada: Hege
el 27 de Jun. de 2020
Hi,
I am new to the Matlab and I want to develop a 10X10X10 voxel(cube) function programme and set all the voxels on the edges "1" and rest(internal voxels)as "0". Then I have to develop a function to calculate the ratio of "1" and "0" and validate my answer using the hand calculations. I have developed a function to find the voxels on the edges(1) and rest as (0).but i am not satisfied with my answer. there is no error.but the answer is wrong(sum of the voxels on the edges and sum of the internal voxels are wrong). I have selected i,j,k values that matches with the voxels on the edges(1) and internal voxels(0) as below programme written by me using the hand skethced diagram.Appreciate your comments.
b=zeros(10,10,10);
for i=1:10
for j=1:10
for k=1:10
if ((i ==1) || (i==2) || (i==3)||(i==4)||(i==5)||(i ==6) || (i==7) || (i==8)||(i==9)||(i==10)) && ((j==1) || (j==10)) && ((k==1) || (k==10))
b(i,j,k)=1;
else
b(i,j,k)=0;
if ((i ==1) || (i==10)) && ((j==2) || (j==3) || (j==4)||(j==5) || (j==6) || (j==7)||(j==8) || (j==9)) &&( (k==1) || (k==10))
b(i,j,k)=1;
else
b(i,j,k)=0;
if ((i ==1) || (i==10)) && ((k==2) || (k==3) || (k==4)|| (k==5) || (k==6) || (k==7)||(k==8) || (k==9)) && ((j==1) || (j==10))
b(i,j,k)=1;
else
b(i,j,k)=0;
end
end
end
end
end
end
sum b(i,j,k)=1
sum b(i,j,k)=0
note---sum b(i,j,k)=1 shown as 695 and sum b(i,j,k)=0 shown as 694 which is totally wrong.(695+694 is not equal to 10X10X10)
0 comentarios
Respuesta aceptada
Steven Lord
el 26 de Jun. de 2020
sum b(i,j,k)=1
sum b(i,j,k)=0
These lines don't do what you think they do. They are equivalent to:
sum('b(i,j,k)=1')
sum('b(i,j,k)=0')
which means they're adding up the ASCII values of the characters in those two char vectors.
There's an easier way to create your desired array. Let's start off with a 10-by-10-by-10 array filled with 0's just as you did.
A = zeros(10, 10, 10);
Now, for all the columns and pages of A, we want to fill in rows 1 and 10 with the value 1. This uses array indexing with element positions. When you read this, read : as "all".
A([1 10], :, :) = 1; % Elements in rows 1 and 10, all columns, all pages of A
We do the same for all rows and pages of A, filling in columns 1 and 10.
A(:, [1 10], :) = 1; % Elements in all rows, columns 1 and 10, all pages of A
And the same for all rows and columns, filling in pages 1 and 10.
A(:, :, [1 10]) = 1; % Elements in all rows, all columns, pages 1 and 10 of A
Now if we've done it right, A should have an 8-by-8-by-8 array of 0's still in the middle. Let's make sure it has the right number of 0's.
nnz(A == 0) % 512 == 8*8*8
The only other values in A are an outer "shell" of 1's.
nnz(A == 1) % 1000 - 512 = 488
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!