Why won't cells populate via if-else statements?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
J. Alex Lee
am 21 Mai 2021
Bearbeitet: J. Alex Lee
am 21 Mai 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,[])
% test the mechanism
x = [1 4 6]
a = reshape(rmap(:,x),[],1)
% now solve your problem
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,:).'
sz = [2,1].*size(Con_Transpose)
T = reshape(rmap(:,Con_Transpose),sz)
2 Kommentare
J. Alex Lee
am 22 Mai 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->?
Siehe auch
Kategorien
Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!