简体   繁体   English

类型如何在MATLAB / Octave中工作?

[英]How do types work in MATLAB/Octave?

I've got a thing called allWords, which for some reason I have to index with curly brackets: 我有一个叫做allWords的东西,由于某种原因,我必须用大括号将其编入索引:

allWords{1}
ans =

  9x1 struct array containing the fields:

    img
    groundTruth

In it are nine images of handwritten characters, and if I assign each of them to a variable: 其中有九个手写字符图像,如果我将它们分别分配给变量:

a=allWords{1}(1).img
b=allWords{1}(2).img
c=allWords{1}(3).img
d=allWords{1}(4).img
e=allWords{1}(5).img
f=allWords{1}(6).img
g=allWords{1}(7).img
h=allWords{1}(8).img
i=allWords{1}(9).img

And do this, then I can see the handwritten word as it was before it was chopped up. 这样做,然后我可以看到手写单词被切碎之前的样子。

imshow([a b c d e f g h i])

That's much nicer than my previous version: 这比我以前的版本好得多:

# the first word
n=1
l=length(allWords{n})
for i = 1:l, subplot(1,l,i); imshow(allWords{n}(i).img); end;

Which puts unnecessary spaces in between the images. 这会在图像之间放置不必要的空间。

However, it's very ugly, how do I generalize it and simplify it? 但是,这非常丑陋,如何将其概括和简化?

This looked like the obvious thing to do, but waah waah, not good enough for sodding Matlab: 这看起来似乎很明显,但是哇,不足以让Matlab湿透:

 imshow(allWords{1}.img)

Stupid heap. 愚蠢的堆。 Has anyone got any tips for finding out the types of things, or working out what types will magically turn into other types and what won't or how the bloody indexing works or any sort of logical explanation why this whole vile mess ended up this way? 有没有人找到任何提示来找出事物的类型,或者弄清楚哪些类型会神奇地变成其他类型,什么不会,或者血腥的索引是如何工作的,或者有任何逻辑上的解释说明为什么整个这种混乱局面会以这种方式结束?

Try: 尝试:

img = cat(2, allWords{1}(:).img);
imshow(img)

Some explanation: 一些解释:

allWords is a cell array. allWords是一个单元格数组。 As far as what you've shown, it contains at least one element. 就您所显示的而言,它至少包含一个元素。

The first element allWords{1} is a structure array of size 9x1 (ie an array where each element is a struct). 第一个元素allWords{1}是大小为9x1的结构数组(即,每个元素都是结构的数组)。 Each structure has two fields img and groundTruth . 每个结构都有两个字段imggroundTruth

Now when you access allWords{1}.img , this will return a comma-separated list, which basically expands into x1,x2,... . 现在,当您访问allWords{1}.img ,这将返回一个逗号分隔的列表,该列表基本上扩展为x1,x2,... So we can combine them into a matrix as [allWords{1}.img] , assuming the sizes are compatible. 因此,假设大小兼容,我们可以将它们组合为[allWords{1}.img]的矩阵。

End result: 最终结果:

imshow([allWords{1}.img])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM