How to use For loop or any other loop to rearrange elements in a matrix using Matlab?

using the attache file for matrix. i am using for loop but i am making mistake some where.
My objective in the columns or rows should contains elements eithr 1 or 3 to generate a boundary of the sphere while the remaining elements should be zero.
For example, if in a row: 3 3 0 0 ... 1, i want first element or last element should be either 1 or 3 but other elments inside of the row or column should be zero. So it should be like: 3 0 0 0 .. 1
Any guidance please. Thanks in advance.
matrix = load('Boundary_closed_1s_3s.txt')
[m, n] = size(matrix)
For i = 1:m
for j = 1:n
if matrix(i,j)==3
else
matrix(i,j)==0
end
end
end

3 Commenti

If you have
0 0 3 3 1 0 1 1 3 0
then what would the expected outcome be?
Then outcome should be like: 0 0 3 0 0 0 0 0 3 0
Regards for reply.
Is the rule that you should alternate taking the first and last of each group? I see places in the file where there are a number of different groups on the same row.

Accedi per commentare.

Risposte (2)

Try this
M = readmatrix('Boundary_closed_1s_3s.txt');
for i = 1:size(M,1)
idx1 = find(M(i,:), 1, 'first');
idx2 = find(M(i,:), 1, 'last');
M(i, idx1+1:idx2-1) = 0;
end
imshow() of matrix before
after

5 Commenti

Dear Ameer Hamza, thanks for your guidance. your code workes horizentally nicely but vertically, the gaps are left open. Therefore, its not possible to generate the closed boundary of the sphere. In every row and colum, there should should be starting and ending as 1 or 3. Using this code, we generate 0s between first colums and last column but there should be also 1 or 3 in the first row and last row of elements. Regards for your reply.
You can apply the same logic vertically
M = readmatrix('Boundary_closed_1s_3s.txt');
for i = 1:size(M,1)
idx1 = find(M(i,:), 1, 'first');
idx2 = find(M(i,:), 1, 'last');
M(i, idx1+1:idx2-1) = 0;
end
for i = 1:size(M,2)
idx1 = find(M(:,i), 1, 'first');
idx2 = find(M(:,i), 1, 'last');
M(idx1+1:idx2-1, i) = 0;
end
The outer boundary should contain either 1 or 3 to be able to generate closed boundary of the sphere. i think, starting rows and ending rows are making problem. Because using code like:
idx1 = find(M(:,i), 1, 'first');
idx2 = find(M(:,i), 1, 'last');
so, we replaced the elements between the first and last colums of the boundary with 0.
i have marked the elements which i dont want because these elements dont represent the boundary of the sphere. i want to repalce them either with 1 or 0. Thanks for all cooperation.
Dr. Ameer, i am trying to utilize your code of find function to introduce 0 between first '1' and last '1', so automatically 3 will be replaced with 0 but its not coming i dont know why.

Accedi per commentare.

The following code is untested.
M = readmatrix('Boundary_closed_1s_3s.txt');
position_of_first = sum(cumprod(~M, 2),2) + 1;
position_of_last = size(M,2) - sum(cumprod(fliplr(~M),2),2);
rows_with_data = find(position_of_last ~= 0);
newM = sparse([rows_with_data, rows_with_data], [position_of_first(rows_with_data), position_of_last(rows_with_data)], 1);
When you examine the results, I suspect you will want to redefine the problem. For example on row 4, that stretch of 1's will be replaced by just the first 1 and the last 1, but that will leave an empty upper boundary for rows 13 and 14.
I would suggest to you that you would be better of taking logical(M) and then using bwskel() https://www.mathworks.com/help/images/ref/bwskel.html or similar morphological operations.

2 Commenti

Thanks Walter for your support. Problem is still there. can we use a for loop to locate the 3s which are coming in the next row or colmun to replace with o. i am thinking how to apply that approach.
Yes, it is possible. However, it is not worth doing considering the option of using bwskel.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 28 Nov 2020

Modificato:

il 29 Nov 2020

Community Treasure Hunt

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

Start Hunting!

Translated by