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

1 回表示 (過去 30 日間)
Kyle Lyman
Kyle Lyman 2021 年 5 月 21 日
コメント済み: J. Alex Lee 2021 年 5 月 22 日
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

回答 (1 件)

J. Alex Lee
J. Alex Lee 2021 年 5 月 21 日
編集済み: J. Alex Lee 2021 年 5 月 21 日
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
% 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,:).'
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 件のコメント
Kyle Lyman
Kyle Lyman 2021 年 5 月 22 日
Hey J. Alex Lee! Thank you for the prompt response. The method that you presented certainly works for matrices that have an even number of elements (i.e. size(con,2)*(size(con,1)-1)=18 in the example you replied to). However, when I change the matrix "con" to have an odd number of elements, I receive the error:
"Error using reshape
Product of known dimensions, 2, not divisible into total number of elements, 9.
Error in DegreeOfFreedom (line 8)
rmap = reshape(1:range,2,[])"
The code that reproduces this error is listed below:
con=[1 2 3; %The first line is local degrees of freedom, thus we focus on the second row through the end row, which sums to 9 total elements.
6 7 9;
4 1 6;
9 7 2]
Con_Transpose=con(2:end,:).'
range=numel(con)-size(con,2);
% generate your "map" from 1->[1;2], 2->[3;4], 4->[7;8], etc.
rmap = reshape(1:range,2,[])
x=[con(2:end,:)];
a = reshape(rmap(:,x),[],1);
sz = [2,1].*size(Con_Transpose);
T = reshape(rmap(:,Con_Transpose),sz)
J. Alex Lee
J. Alex Lee 2021 年 5 月 22 日
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->?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by