# Why won't cells populate via if-else statements?

1 view (last 30 days)
Kyle Lyman on 21 May 2021
Commented: J. Alex Lee on 22 May 2021
I am taking the transpose of the last three rows of matrix "con". What I want to do is search each newly transposed column for specific numbers using find() (i.e. 1,2,3,4,5,6,7,8,9). I want each of these specific numbers to correspond to a 2x1 matrix, each of which is to be inserted into a cell structure. Then I want to use cell2array to form a matrix. For example [1;2;3;4;5;6] maps to [1;2;3;4;5;6;7;8;9;10;11;12]. My issue is that when I run the code T is displaying as an empty matrix (T=[ ]). I have attached the code down below. Thank you in advance!
clc
clear all
close all
con=[1 2 3 4 5 6; %Connectivity matrix
1 2 3 4 5 6;
3 5 4 8 7 9;
4 5 6 2 5 7];
Con_Transpose=con(2:end,:).' %Transpose of last three rows of connectivity matrix
width=1:1:size(Con_Transpose,2);
for i=1:length(width)
a={};
if Con_Transpose(:,width(i))==1;
a{find(Con_Transpose==1,1),1}=[1;2]
end
if Con_Transpose(:,width(i))==2;
a{find(Con_Transpose==2,1),1}=[3;4]
end
if Con_Transpose(:,width(i))==3;
a{find(Con_Transpose==3,1),1}=[5;6]
end
if Con_Transpose(:,width(i))==4;
a{find(Con_Transpose==4,1),1}=[7;8]
end
if Con_Transpose(:,width(i))==5;
a{find(Con_Transpose==5,1),1}=[9;10]
end
if Con_Transpose(:,width(i))==6;
a{find(Con_Transpose==6,1),1}=[11;12]
end
if Con_Transpose(:,width(i))==7;
a{find(Con_Transpose==7,1),1}=[13;14]
end
if Con_Transpose(:,width(i))==8;
a{find(Con_Transpose==8,1),1}=[15;16]
end
if Con_Transpose(:,width(i))==9;
a{find(Con_Transpose==9,1),1}=[17;18]
end
T=cell2mat(a)
end

J. Alex Lee on 21 May 2021
Edited: J. Alex Lee on 21 May 2021
The if statements are probably your problem...you are comparing a column vector with scalars, which off the top of my head I don't know the behavior of.
I won't attempt to fix your code, but consider this strategy that's based on indexing, doesn't need to loop, and doesn't touch cell arrays...does it do what you want:
% generate your "map" from 1->[1;2], 2->[3;4], 4->[7;8], etc.
rmap = reshape(1:18,2,[])
rmap = 2×9
1 3 5 7 9 11 13 15 17 2 4 6 8 10 12 14 16 18
% test the mechanism
x = [1 4 6]
x = 1×3
1 4 6
a = reshape(rmap(:,x),[],1)
a = 6×1
1 2 7 8 11 12
con=[1 2 3 4 5 6; %Connectivity matrix
1 2 3 4 5 6;
3 5 4 8 7 9;
4 5 6 2 5 7];
Con_Transpose=con(2:end,:).'
Con_Transpose = 6×3
1 3 4 2 5 5 3 4 6 4 8 2 5 7 5 6 9 7
sz = [2,1].*size(Con_Transpose)
sz = 1×2
12 3
T = reshape(rmap(:,Con_Transpose),sz)
T = 12×3
1 5 7 2 6 8 3 9 9 4 10 10 5 7 11 6 8 12 7 15 3 8 16 4 9 13 9 10 14 10
##### 2 CommentsShowHide 1 older comment
J. Alex Lee on 22 May 2021
So what does your conversion map look like...
1->1,2
2->3,4
3->5,6
4->7,8
5->9
6->?
7->?
8->?
9->?